Nio2 Flashcards

0
Q

If you had tried to read the contents of that file represented by the Path before it was created,

A

you’d get an IOException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

You can create (and work with) a Path that isn’t immediately bound to a corresponding physical location…

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

in the NIO.2 APIs, the only checked exception thrown by the various methods in Path or Paths is an IOException

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Paths.get(“/usr/bin/zip”) call is equivalent to calling the following longer sequence…

A

Path listing = FileSystems.getDefault().getPath(“/usr/bin/zip”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

While Copying the file from source to target. CopyOption could be …

A

REPLACE_EXISTING, COPY_ATTRIBUTES, or NOFOLLOW_LINKS. Can throw exceptions such as FileAlreadyExistsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

You can check the ability of a program to read, write, or execute programmatically using…

A

The Files class provides the methods isReadable(), isWriteable(), and isExecutable() to do that

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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…

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

If you do not specify the correct view name in the second attribute of get attribute method,

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Java 7 offers a solution: an API— readAttributes()—to read the attributes in one shot. The API comes in two flavors:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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…

A

extend your implementation from the SimpleFileVisitor class. In this way, you can simply override those methods that you want to customize

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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:

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

You can get a watch service instance using the …

A

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())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Once you have an instance of the watch service, the next step is to register the directory to the watch service.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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:

A
• • 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
16
Q

The …design pattern is used to enable walking through a file tree

A

Visitor

17
Q

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

A

Yes

18
Q

InvalidPathException is a RuntimeException…

A

Yes

19
Q

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

A

Yes