NIO.2 Flashcards

1
Q

Which is the entry point for working with NIO.2?

A

java.nio.file.Path

It represents an heirarchical path to the storage system to a file or directory

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

What is an additional feature availale with the Path in comparision to the File?

A
  1. It has all the features that java.io.File has

2. It also has support for Symbolic links that represents the pointer to another file system

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

Why is Path an interface in Java if it is the entry point?

Means, why it is not a class so that we could have got the object for it?

A

Creating a file or a directory is the file-system dependent task in NIO.2.
So, when we get a path object in NIO.2, JVM returns a system specific details for current platform.

Otherwise, we would have to know underlying system platform and develop the code

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

Name a helper class whose primary objective is to operate on Path object?

A

java.nio.file.Files, whose primary objective is to operate on instances of Path object

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

What are the two ways to create a Path object using Paths Class?

A
  1. Path path = Paths.get(“c:\”);
  2. Path path = Paths.get(String, String..)

The second one allows us to create a path from the list of String arguments in which the OS system-dependent separator is automatically inserted between the String arguments.

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

How can we get the file separator using Path?

A

System.getProperty(“path.separator”);

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

Way to create a Path object from URI?

A

Path path =Paths.get(new URI(“file:///……/../../../.txt”);

URI are string of characters that indicates a resource type. We have to start with schema name. Eg.. http://, ftp://

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

Path path = Paths.get(“ftp://hello/dilli.txt”);

What is the path here?

A

It throws runtime exception.

We must only mention absolute path

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

How to convert a Path instance to the URI instance?

A

Path path4 = Paths.get(new URI(“http://hellow.com));

URI uri = path4.toUri();

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

What is the exception thrown by new URI constructor?

A

URISyntaxException

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

What is the other way of accessing the Path object without Paths.get()?

A

Path path = FileSystems.getDefault().getPath(URI);

or

Path path = FileSystems.getDefault().getPath(String, String…)

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

what is the advantage of using FileSystems for getting the Path object?

A
  1. It gives us the ability to connect to the remote file systems.
  2. When we want to interact frequently with the remote file systems we have to use this technique.

FileSystem fileSystem = FileSystems.getfileSystm(new URI(“http://google.com”));

Path path = fileSystem.getPath(hello.txt);

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

How the Path interface works with legay file system?

A
  1. Converting File object to path:

Path path = file.toPath()

Converting Path to legacy File object:

File file = path.toFile();

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

which methods represents a String representation of a entir path?

A

toString() method

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

Which is the only method in the Path interface that returns a string?

A

toString()

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

What is the output of followinig code:

Path path = Paths.get(“C:\Dilli\Dilli.txt”);
System.out.println(path.toString());

for(int i=0; i

A

C:\Dilli\Dilli.txt
Element 0 is Dilli
Element 1 is Dilli.txt

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

Will the root element be included in the return values of getName() method?

A

No, root element will not be included.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
  1. What is the getName(int) index starts from? 0-based or 1-based?
A
  1. 0-based
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What type is the getName(int) method returns?

A

Path object

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

Which method returns the FileName of the Path? What type does it returns>

A

path.getFileName().

It returns a Path object

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

getParent() method?

A

returns the Parent path or null if there is no parent.

Eg.., /zoo/hello/tiger.txt

parent of tiger.txt is /zoo/hello
parent of hello is /zoo
parent of zoo is /

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

getRoot() method?

A
  1. It returns the Root element of the path.

2/ Returns null if the path is absolute

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

how to check whether the path object is an absolute pr not?

A

isAbsolute() method. returns true if the path is absolute

returns false if it is not

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

What is toAbsolutePath() method does?

A
  1. Converts a given relative path into an absolute path by appending the current working directory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What if the given path is already absolute path? What does toAbsolutePath() returns?
1. It returns an another path object which is the copy of the given absolute path
26
System.out.println(Paths.get("/hello/ganesh/Dilli.txt").isAbsolute()); System.out.println(Paths.get("C:/hello/ganesh/Dilli.txt").isAbsolute()); What is the output of this when runs in Windows?
1. false | 2. true
27
what is subpath method? what are the arguments it can take
Subpath returns a relative subpath of the Path object. It takes two int values, an index start and index end
28
What is the output? Path path1 = Paths.get("/mammal/carnivore/racoon.image"); 1. System.out.println(path1.subpath(1, 3)); 2. System.out.println(path1.subpath(0, 3)); 3. System.out.println(path1.subpath(0, 4)); 4. System.out.println(path1.subpath(1, 1));
1. carnivore/racoon.image 2. mammal/carnivore/racoon.image 3. IllegalArgumentException (RunTimeException) 4. IllegalArgumentException (RunTimeException)
29
What needs to be kept on mind for indexing the elements in subpath?
1. The root of the Path is not included in the subpath return value 2. The directory next to root is the 0-th index element
30
What are Path symbols?
1) . -> refers to the current directory | 2) .. -> refers to the Parent directory (one up directory)
31
A very cool method to get relation between two Path systems?
relativize(path);
32
Does the fileExists method throws an Exception?
No, it doesnt throw any exception
33
What is isSameFile() method does?
useful for determining if two path objects relate to same file. It also checks if two path objects relate to same directory
34
When does actual checking of whether file exists happen in isSameFile() method?
1. isSameFile() method first checks if the path objects are equal in terms of equal() method, if yes, it automatically returns true without the check of whether file exists. 2. If the path objects equals() return false, it locates files in each Path object and determines if they are true, throws IOException if any file doesnt exists
35
How to create directories with NIO.2 API?
Files.createDirectory(path) | Files.createDirectories(path)
36
Does the directory creation throws an exception?
1. If the directory cannot be created 2. If the directory already exists it throws checked IOException
37
What is the way to copy a file in NIO.2?
Files.copy(path, path) and it throws IOException
38
What should be kept in mind regarding copying Directories.
Directory copy are shallow rather than deep. Files and Directories within the directories are not copied. If we need to copy them, we have to traverse them
39
Copying the files with java.io and NIO.2?
two overloaded methods. 1. Files.copy(java.io.InputStream object, path) 2. Files.copy(path, java.io.OutputStream)
40
Which overloaded method supports using the varargs?
1. Files.copy(inputStream, path)
41
What is special about move() method?
moves or rename the files or directory within the file system
42
What is the default functionality of the move() method?
1. move() method follows the links 2. move method throws exception if file already existe 2. it does not doe atomic move
43
How to change the default behaviour of the move() method?
NOFOLLOW_LINKS, REPLACE_EXISTING, ATOMIC_MOVE
44
What if the atomic move is not supported in file systm
AtomicMoveNotSupportedException
45
What are the several restrictions on the movig of directories?
1. move method can be applied to non-empty directories only if they are on same underlying drive 2. Moving of empty directories across drives are supported 3. Moving of non empty directory across drie throws DirectoryNotEmptyException
46
What does readAllLines() method return?
Ordered list of String values. Each List contains the String
47
What are the three methods to find whetehr a path is 1. directory 2. a regular file and 3. Symbolic link
1. Files.isDirectory(Path) 2. Files.isRegularFile(Path) 3. Files.isSymbolicLink(Path)
48
is it possible for the method isRegularFile(Path) to return true for Symbolic links.
Yes, it can return true as long as the Symbolic link resolves to a regular file
49
Does isDirectory(), isRegularFile() or isSymbolicLink() throws Exception?
No, they dont throw exceptions
50
How to find whether a file is hidden in fileSystem?
Files.isHidden() method. It throws IOException
51
How to find whether a File/Dircetory is readable or executable?
Files.isReadable(Path) Files.isExecutable(Path) It does not thow any exceptions
52
What is the functionality of Files.size(Path)?
It returns the size of the files in Bytes
53
How to get the last modified time of the file system?
Files.getLastModifiedTime(Path) which returns a FileTime object to accomplish the task. FileTime class isa simple container class that stores the information of date/time
54
What is the method that returns the last modified time in epoch?
FileTime has toMillis() method
55
How to get the owner of a directory?
Files.getOwner(path) It returns an instance of UserPrincipal that represents the owner of the file withini the file system
56
How to set the owner of the directory?
Files.setOwner(path, UserPrincipal)
57
Do the setOwner and getOwner method throws any exception?
IOException in case of any issues accessing or modifying the ownership
58
How to set the owner of a file to an arbitrary user in the fileSystem
UserPrincipalOwner owner = FileSystems.getDefault(). getUserPrincipalLookupService().lookupPrincipalByName(user); NIO2 API provides a UserPrincipalLookupService helper class for finding the UserPrincipal record for a particular user. We need to get the instance of fileSystem with which get the instance of UserPrincipalLookupService to get the UserPrincipal name
59
What is a view in fileSystems?
A group of related attributes for a particular fileSystem type. A file may support multiple views allowing you to retrieve and update various sets of information
60
What are the parameters a method need to request a view.
1. Path of the file/Directory whose information you want to read 2. A class object, which tells the NIO.2 API which type of view we would like to get returned
62
What are the attributes Classes and View Classes?
1. BasicFileAttributes, BasicFileAttributeView 2. DosFileAttributes, DosFileAttributeView 3. PosixFileAttributes, PosixFileAttributeView
63
What is BasicFileAttributes
All attributes classes extends from BasicFileAttributes class, so they contain all attributes common to all supported Operating systems
64
What is BasicFileAttributesView
BasicFileAttributeView is used to modify a set of date/time values
65
What are the basic file Attributes that can be modified?
Only Timme and Date pair
66
What are the two strategies associated with Walking a directory tree?
1. Depth-first search | 2. Breadth-first search
67
Which strtaegy of walking is used by Streams API?
depth-first searching with default Maximum depth of Integer.MAX_VALUE
68
what is the method (first method) to traverse a directory and what does it returns?
Files.walk(path) that returns a Stream object that traverses a directcory in depth first manner
69
what method does the Files.walk(path) method follows? Lazy or eager?
Lazy. Until a directory is traversed, its file contents will not be loaded
70
How many iterations does the walk method iterates by default and how can we change it?
It iterates to the maximum of INTEGER.MAX_VALUE. There is a overloaded version Files.walk(path, int) that says how many iterations it must do
71
Does the walk method traverse symbolic links by default? How to change the default behaviur?
It does not traverse Symbolic links by default as it may cause cycles (infinite loops). We can change this default behaviour by passing 'FOLLOW_LINKS' option as vararg to walk method
72
Does the walk method traverse symbolic links by default? How to change the default behaviur?
It does not traverse Symbolic links by default as it may cause cycles (infinite loops). We can change this default behaviour by passing 'FOLLOW_LINKS' option as vararg to walk method. If the option is used, the walk()m method will keep track of the path it has traversed, so if it traverse again it throws FilseSystemLoopException
73
What is the other method that is similar to walk method in a way?
find method. Files.find(path, int, BiPredicate) It requires us to explicitly specify the depth value. It also supports FOLLOW_LINK vararg.
74
What is the alternative method in java,io.File listFiles() method in NIO.2?
Files.list(path). It outputs the full path of the file in the specified directory
75
What is the coolest alternative for readAllLines() and how it is coolest?
Files.lines(path). It returns a Stream objectc It doesnt load entire data into memory as it reads and process lazily.
76
readAllLines() and lines(). What does they return?
readAllLines() -> List of String | lines() -> Stream object
77
Can we call forEach on the return list of readAllLines()?
Yes, as Collections supports calling forEach.