java 2 3
fdsa
fdsa
-
- 1 / 496
-
Flashcards
Which Signatures of resolve exists?
- public Path resolve(Path other)
- public Path resolve(String other)
How does relativize work with relative Paths, how with absolute?
If both relative: computes like they are in the samye working directory
alternatively if both absoulte: computes the relative path from one to another, regardless of current working directory
if mixed a exception gets thrown
What happens with relativize if two absolute Paths doesnt have the same rootDir?
throw exception
What happens here?
var p3 = Path.of("../../fish.txt");
System.out.println(p3.normalize());
../../fish.txt
What happens here?
var p1 = Paths.get("/pony/../weather.txt");
var p2 = Paths.get("/weather.txt");
System.out.println(p1.equals(p2));
System.out.println(p1.normalize().equals(p2.normalize()));
false, true
What does toRealPath() do, whats the syntax?
public Path toRealPath(LinkOption... options) throws IOException
Eliminates any redundant path symbols, join the path with the curent working directory if the path is relative
Does toRealPath check if the file exists?
no, throws ioException
Does toRealPath follow symbolicLinks?
Yes
What happens here? Symbolic link from /zebra to /horse and current working direectory is /horse/schedule
System.out.println(Paths.get("/zebra/food.txt").toRealPath());
System.out.println(Paths.get(".././food.txt").toRealPath());
Both times the output is: /horse/food.txt
Does files have methods like mkdir/renameTo?
No but move() and createDirectory()
Whats the signature of Files.exists?
public static boolean exists(Path path, LinkOption... options)
Does Files.isSameFile follow symbolicLinks?
Yes
Can Files.isSameFile also check directories?
yes
What happens in isSameFile when a Path not exists? Are there exceptions to this?
Will trigger an exception, specialcase: If two path objects are equal in terms of equals() then it wont check if the file exists
Does Files.isSameFile compare the contents?
No
How to create a Directory with Files-class? (Methods)
- public static Path createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException
- public static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException
What happens with createDirectories if the directories already exists?
Will simply doe nothing -> createDirectory would throw an excetion
What happens with createDirectory if it already exists? What if the path leading up tp the directory does not exists?
throw an exception
What is the signature of Files.copy?
public static Path copy(Path source, Path target, CopyOption... options) throws IOException
Whats the default behavoiur of copy if the target already exists?
Throws an exception
What happens here if food.txt and /enclosure exists?
var file = Paths.get("food.txt");
var directory = Paths.get("/enclosure");
Files.copy(file, directory);
Throws an exception -> enclosure already exists -> Path would have to be "/enclosure/food.txt"
What happens with Files.delete if a directory is not empty?
Throws an exception
What happens if delete/deleteIf is called on a symbolic link?
The link will be deleted, not the target
What does newBufferedReader() do?
Reads the file specified at the Path location
What does Files.newBufferedWriter(Path path) do?
Writes to a file specified at the Path location
What is the signature of Files.readallLines?
public static List<String> readAllLines(Path path) throws IOException
Whats the problem with Files.readAllLines()?
May trigger an outofMemoryError
What is a regular file?
One that contains content -> no symbolic links
Does Files.isRegularFile check the target of symbolicLinks?
Yes
Which of these functions in Files will throw an exceiption if the path does not exist?
- isHedden, isReadable, isWritable, isExecutable
isHidden
What happens with Files.size on directories?
is undefined, depends on the filesystem
Whats the difference between BasicFileAttributes and BasicFileAttributeView?
BasicFileAttributes are readOnly, BasicFileAttributeView is also updatable
Whats the signature of readAttributes?
public static <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException
Signature of Files.getFileAttributeView?
public static <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options)
Can all file attributes be changed?
No, f.e. you can't change a file to a directory
Whats the signature of the File.list method?
public static Stream<Path> list(Path dir) throws IOException
Are symbolic links follow when using Files.list?
No
How deep traverses Files.list()?
only a single directory
Is Files.walk breadth-first or depth-first?
depth-first
Syntax of Files.walk()?
- public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException
- public static Stream<Path> walk(Path start, int maxDepth, FileVisitOption... options) throws IOException