Java fila NIO.2 Flashcards
How do you get an attribute of a file?
Object object = Files.getAttribute(path, “creationTime”,
LinkOption.NOFOLLOW_LINKS);
What is the difference between File and Files?
File is an old class (java 4) that represents file/directory path names, whereas Files was introduced in java 7 as a utility class with comprehensive support for IO APIs
What is the difference between Path and Paths?
The path interface represents a file/directory path and defines a useful list of methods. Paths is a utility class that offers only two methods ( both to get the path object)
What is the Files.lines() used for?
Read contents of a file
What is a Path?
Programming abstraction to represent the path of a file/directory
How do you get the instance of a path?
using the get() method of the Paths class
What does the Files.list(Path path) method returns?
Stream{Path}
It is like a walk() method with dept limit to 1, it is analogous to File.listFiles()
What does the Files.walk(Path path) method return?
Stream{Path}. It will not traverse symbolic links by default
What is the difference between Files.list() and Files.walk()?
.list() does not recursively traverse the directories while .walk() does
What does the Files.lines() returns?
Stream{Lines}
Given:
File file = new File(“x”);
how can you get a instance of Path?
file.toPath();
What are the common optional arguments in NIO2 and its usages?
EnumValue:Usage
- NOFOLLOW_LINKS : test file exists, read file data, copy, move file
- FOLLOW_LINKS : transverse a directory tree
- COPY_ATTRIBUTES : copy file
- REPLACE_EXISTING: copy file/ move file
- ATOMIC_MOVE: move file
What does the toAbsolutePath() does?
Returns the absolute path of this path. If the path is already absolute, then it just returns it
What does the relativize(String other) method do?
constructs a relative path between the path and the given path.
- It requires that both paths be absolute or both relative, or else it will throw a new IllegalArgumentException
- If this path and the given path are equal then an empty path is returned
- A relative path cannot be constructed if only one of the paths have a root component.
- Where both paths have a root component then it is implementation dependent if a relative path can be constructed.
What does the resolve(Path path)
resolve(String string) method do?
- adds two paths
- if the other is a absolute path, just returns other
- if the other path is empty, just return THIS path
- where the given path has a root component then resolution is highly implementation dependent and therefore unspecified.