NIO.2 Flashcards Preview

Java8 > NIO.2 > Flashcards

Flashcards in NIO.2 Deck (77)
Loading flashcards...
1

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

java.nio.file.Path

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

2

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

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

3

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?

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

4

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

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

5

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

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.

6

How can we get the file separator using Path?

System.getProperty("path.separator");

7

Way to create a Path object from URI?

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://

8

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

What is the path here?

It throws runtime exception.

We must only mention absolute path

9

How to convert a Path instance to the URI instance?

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

URI uri = path4.toUri();

10

What is the exception thrown by new URI constructor?

URISyntaxException

11

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

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

or

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

12

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

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

13

How the Path interface works with legay file system?

1. Converting File object to path:

Path path = file.toPath()

Converting Path to legacy File object:

File file = path.toFile();

14

which methods represents a String representation of a entir path?

toString() method

15

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

toString()

16

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

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

17

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

No, root element will not be included.

18

1. What is the getName(int) index starts from? 0-based or 1-based?

1. 0-based

19

What type is the getName(int) method returns?

Path object

20

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

path.getFileName().

It returns a Path object

21

getParent() method?

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 /

22

getRoot() method?

1. It returns the Root element of the path.
2/ Returns null if the path is absolute

23

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

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

24

What is toAbsolutePath() method does?

1. Converts a given relative path into an absolute path by appending the current working directory

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)