Nio2 Flashcards
If you had tried to read the contents of that file represented by the Path before it was created,
you’d get an IOException
You can create (and work with) a Path that isn’t immediately bound to a corresponding physical location…
Yes
in the NIO.2 APIs, the only checked exception thrown by the various methods in Path or Paths is an IOException
Yes
Paths.get(“/usr/bin/zip”) call is equivalent to calling the following longer sequence…
Path listing = FileSystems.getDefault().getPath(“/usr/bin/zip”);
While Copying the file from source to target. CopyOption could be …
REPLACE_EXISTING, COPY_ATTRIBUTES, or NOFOLLOW_LINKS. Can throw exceptions such as FileAlreadyExistsException
You can check the ability of a program to read, write, or execute programmatically using…
The Files class provides the methods isReadable(), isWriteable(), and isExecutable() to do that
second parameter of the getAttribute() method. You need to provide a correct attribute name to extract the associated value. The expected string should be specified in…
view:attribute format, where view is the type of FileAttributeView and attribute is the name of the attribute supported by view. If no view is specified, the view is assumed as basic
If you do not specify the correct view name in the second attribute of get attribute method,
you will get an UnsupportedOperationException, and if you mess up with the attribute name, you will get an IllegalArgumentException.
For example, if you type sized instead of size, you’ll get this exception: Exception in thread “main” java.lang.IllegalArgumentException: ‘sized’ not recognized [ . . . stack trace elided
Java 7 offers a solution: an API— readAttributes()—to read the attributes in one shot. The API comes in two flavors:
Map readAttributes(Path path, String attributes, LinkOption. . . options)
<a> A readAttributes(Path path, Class<a> type, LinkOption. . . options</a></a>
The method returns an instance from the BasicFileAttributes hierarchy. The file attributes hierarchy is shown in Figure 9-1. The BasicFileAttributes is the base interface from which DosFileAttributes and PosixFileAttributes are derived
If you move a symbolic link, the link itself will be moved, not the target file of the link. It is important to note that in the case of the copy() method, if you specify a symbolic link, the target of the link is copied, not the link itself
Yes
The file you intend to delete must exist;
otherwise you will get a NoSuchFileException. If you silently delete a file and do not want to be bothered about this exception, then you may use the deleteIfExists() method, which will not complain if the file does not exist and deletes it if the file exists
Yes
You need to implement the FileVisitor interface so that you can create an instance of your implementation and pass it to the walkFileTree() methods. However, if you do not want to implement all four methods in the FileVisitor interface, you can simply…
extend your implementation from the SimpleFileVisitor class. In this way, you can simply override those methods that you want to customize
One more thing that requires attention here is the FileVisitReturn value. You can control the flow of the walk using FileVisitReturn values. There are four types of different return values:
- CONTINUE: It indicates that the walk through the file tree should continue.
- TERMINATE: It indicates that the walk through the file tree should be terminated immediately.
- SKIP_SUBTREE: It indicates that the rest of the subtree should be skipped for the walking file tree.
- SKIP_SIBLINGS: It indicates that walking file tree should be stopped for the current directory and its sibling directories. If it is returned from the preVisitDirectory(), then the containing files/directories are not visited and the postVisitDirectory() is also not visited. If it is returned from visitFile(), then no further file in the directory is visited. If it is returned from the postVisitDirectory(), then siblings of the directory are not visited
You can get a watch service instance using the …
FileSystem class. you are getting a FileSystem instance using a path instance, and then you are requesting an instance of watch service from the FileSystem. You may also get an instance of the FileSystem from FileSystems (FileSystems.getDefault())
Once you have an instance of the watch service, the next step is to register the directory to the watch service.
The Path object provides two methods for registration: the first register() method takes variable arguments (first an instance of watch service and subsequently the kind of watch event in which you are interested). The second register() method takes one additional parameter—the watch event modifier
In the loop, you need to wait for the event to happen. Here, you can ask the watch service to notify this program when an event occurs. You can do this using three methods:
• • The The poll() method returns a queued key if available; otherwise it returns immediately. poll(long, TimeUnit) method returns a queued key if available; otherwise it waits for the specified time (the long value) and for the specified time unit. The method returns after the specified time limit is elapsed The take() method returns a queued key if available; otherwise it waits until a key is available. The key difference between the poll() and take() methods is that poll() is a non-blocking call and take() is a blocking call
The …design pattern is used to enable walking through a file tree
Visitor
If you are watching a directory using the watch service offered by Java 7, then only files contained in that directory will be watched—and not the files contained in the subdirectories of that directory. If you intend to watch the whole subtree of the file system, you need to recursively register each directory in the subtree
Yes
InvalidPathException is a RuntimeException…
Yes
the name that is closest to the root has index 0; note that the root itself (in this case D:) is not considered as an element in the path
Yes