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.
True or false?
Files belong to java.io while File belong to NIO2?
False,
File belongs to legacy java.io while Files belong to the NIO2
What does the Files.move(Path path, Path path) do?
moves or renames a file, throws IOException
What does the Files.delete(Path path) do?
deles a file or EMPTY directory within the file system. It throws IOException under a variety of circumstances
(if it is a non empty directory)
What does the Files.readAllLines(Path path, ?Charset) return?
reads the lines of a text file and returns the results as an ordered List of String. Throws IOException, can pass an option charset param
Regarding to file attributes…
what is a view?
A view is a group of related attributes for a particular file system type
What does the Files.readAttributes(Path path, Class{a}, LinkOption options) returns?
returns a read only view of the file attributes. May throw Exception
What does the
Files.getFileAttributeView(Path path,Class type, LinkOption… options) return?
return the underlying attribute view and it provides a direct resource for modifying file information. May throw Exception
What are the attributes and view classes?
Attributes class: View class
BasicFileAttributes: BasicFileAttributeView
DosFileAttributes:DosFileAttributeview
PosixFileAttributes: PosixFileAttributeView
What does the Files.find(Path, int, Bipredicate) does?
behaves in a similar maner as the Files.walk(). Returns a Stream{Path}