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
Q

What if the given path is already absolute path? What does toAbsolutePath() returns?

A
  1. It returns an another path object which is the copy of the given absolute path
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

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?

A
  1. false

2. true

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

what is subpath method? what are the arguments it can take

A

Subpath returns a relative subpath of the Path object.

It takes two int values, an index start and index end

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

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));
A
  1. carnivore/racoon.image
  2. mammal/carnivore/racoon.image
  3. IllegalArgumentException (RunTimeException)
  4. IllegalArgumentException (RunTimeException)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What needs to be kept on mind for indexing the elements in subpath?

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

What are Path symbols?

A

1) . -> refers to the current directory

2) .. -> refers to the Parent directory (one up directory)

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

A very cool method to get relation between two Path systems?

A

relativize(path);

32
Q

Does the fileExists method throws an Exception?

A

No, it doesnt throw any exception

33
Q

What is isSameFile() method does?

A

useful for determining if two path objects relate to same file.

It also checks if two path objects relate to same directory

34
Q

When does actual checking of whether file exists happen in isSameFile() method?

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

How to create directories with NIO.2 API?

A

Files.createDirectory(path)

Files.createDirectories(path)

36
Q

Does the directory creation throws an exception?

A
  1. If the directory cannot be created
  2. If the directory already exists

it throws checked IOException

37
Q

What is the way to copy a file in NIO.2?

A

Files.copy(path, path)

and it throws IOException

38
Q

What should be kept in mind regarding copying Directories.

A

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
Q

Copying the files with java.io and NIO.2?

A

two overloaded methods.

  1. Files.copy(java.io.InputStream object, path)
  2. Files.copy(path, java.io.OutputStream)
40
Q

Which overloaded method supports using the varargs?

A
  1. Files.copy(inputStream, path)
41
Q

What is special about move() method?

A

moves or rename the files or directory within the file system

42
Q

What is the default functionality of the move() method?

A
  1. move() method follows the links
  2. move method throws exception if file already existe
  3. it does not doe atomic move
43
Q

How to change the default behaviour of the move() method?

A

NOFOLLOW_LINKS,
REPLACE_EXISTING,
ATOMIC_MOVE

44
Q

What if the atomic move is not supported in file systm

A

AtomicMoveNotSupportedException

45
Q

What are the several restrictions on the movig of directories?

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

What does readAllLines() method return?

A

Ordered list of String values.

Each List contains the String

47
Q

What are the three methods to find whetehr a path is

  1. directory
  2. a regular file and
  3. Symbolic link
A
  1. Files.isDirectory(Path)
  2. Files.isRegularFile(Path)
  3. Files.isSymbolicLink(Path)
48
Q

is it possible for the method isRegularFile(Path) to return true for Symbolic links.

A

Yes, it can return true as long as the Symbolic link resolves to a regular file

49
Q

Does isDirectory(), isRegularFile() or isSymbolicLink() throws Exception?

A

No, they dont throw exceptions

50
Q

How to find whether a file is hidden in fileSystem?

A

Files.isHidden() method.

It throws IOException

51
Q

How to find whether a File/Dircetory is readable or executable?

A

Files.isReadable(Path)
Files.isExecutable(Path)

It does not thow any exceptions

52
Q

What is the functionality of Files.size(Path)?

A

It returns the size of the files in Bytes

53
Q

How to get the last modified time of the file system?

A

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
Q

What is the method that returns the last modified time in epoch?

A

FileTime has toMillis() method

55
Q

How to get the owner of a directory?

A

Files.getOwner(path)

It returns an instance of UserPrincipal that represents the owner of the file withini the file system

56
Q

How to set the owner of the directory?

A

Files.setOwner(path, UserPrincipal)

57
Q

Do the setOwner and getOwner method throws any exception?

A

IOException in case of any issues accessing or modifying the ownership

58
Q

How to set the owner of a file to an arbitrary user in the fileSystem

A

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
Q

What is a view in fileSystems?

A

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
Q

What are the parameters a method need to request a view.

A
  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
61
Q

What methods does the Files API includes for accessing View informations? Their information

Do they throw any Exceptions and their importance?

A

Files.readAttributes(path, Class<a>) - that returns read-only view of the file attributes</a>

Files.getFileAttributeView(path, Class</a><a>) returns the underlying attribute view and it provides the direct resource for modifying the information</a>

Both methods throw IOException when a view class type is not supported.

Eg.., Trying to read windows based attributes within the LINUX file system may throw an UnsupportedOperationException</a>

62
Q

What are the attributes Classes and View Classes?

A
  1. BasicFileAttributes, BasicFileAttributeView
  2. DosFileAttributes, DosFileAttributeView
  3. PosixFileAttributes, PosixFileAttributeView
63
Q

What is BasicFileAttributes

A

All attributes classes extends from BasicFileAttributes class, so they contain all attributes common to all supported Operating systems

64
Q

What is BasicFileAttributesView

A

BasicFileAttributeView is used to modify a set of date/time values

65
Q

What are the basic file Attributes that can be modified?

A

Only Timme and Date pair

66
Q

What are the two strategies associated with Walking a directory tree?

A
  1. Depth-first search

2. Breadth-first search

67
Q

Which strtaegy of walking is used by Streams API?

A

depth-first searching with default Maximum depth of Integer.MAX_VALUE

68
Q

what is the method (first method) to traverse a directory and what does it returns?

A

Files.walk(path) that returns a Stream object that traverses a directcory in depth first manner

69
Q

what method does the Files.walk(path) method follows? Lazy or eager?

A

Lazy. Until a directory is traversed, its file contents will not be loaded

70
Q

How many iterations does the walk method iterates by default and how can we change it?

A

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
Q

Does the walk method traverse symbolic links by default? How to change the default behaviur?

A

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
Q

Does the walk method traverse symbolic links by default? How to change the default behaviur?

A

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
Q

What is the other method that is similar to walk method in a way?

A

find method.

Files.find(path, int, BiPredicate)

It requires us to explicitly specify the depth value. It also supports FOLLOW_LINK vararg.

74
Q

What is the alternative method in java,io.File listFiles() method in NIO.2?

A

Files.list(path).

It outputs the full path of the file in the specified directory

75
Q

What is the coolest alternative for readAllLines() and how it is coolest?

A

Files.lines(path). It returns a Stream objectc

It doesnt load entire data into memory as it reads and process lazily.

76
Q

readAllLines() and lines(). What does they return?

A

readAllLines() -> List of String

lines() -> Stream object

77
Q

Can we call forEach on the return list of readAllLines()?

A

Yes, as Collections supports calling forEach.