NIO.2 Flashcards
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
What is an additional feature availale with the Path in comparision to the File?
- 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
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
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
What are the two ways to create a Path object using Paths Class?
- Path path = Paths.get(“c:\”);
- 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 can we get the file separator using Path?
System.getProperty(“path.separator”);
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://
Path path = Paths.get(“ftp://hello/dilli.txt”);
What is the path here?
It throws runtime exception.
We must only mention absolute path
How to convert a Path instance to the URI instance?
Path path4 = Paths.get(new URI(“http://hellow.com));
URI uri = path4.toUri();
What is the exception thrown by new URI constructor?
URISyntaxException
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…)
what is the advantage of using FileSystems for getting the Path object?
- It gives us the ability to connect to the remote file systems.
- 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 the Path interface works with legay file system?
- Converting File object to path:
Path path = file.toPath()
Converting Path to legacy File object:
File file = path.toFile();
which methods represents a String representation of a entir path?
toString() method
Which is the only method in the Path interface that returns a string?
toString()
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
Will the root element be included in the return values of getName() method?
No, root element will not be included.
- What is the getName(int) index starts from? 0-based or 1-based?
- 0-based
What type is the getName(int) method returns?
Path object
Which method returns the FileName of the Path? What type does it returns>
path.getFileName().
It returns a Path object
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 /
getRoot() method?
- It returns the Root element of the path.
2/ Returns null if the path is absolute
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
What is toAbsolutePath() method does?
- Converts a given relative path into an absolute path by appending the current working directory
What if the given path is already absolute path? What does toAbsolutePath() returns?
- It returns an another path object which is the copy of the given absolute path
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?
- false
2. true
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
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));
- carnivore/racoon.image
- mammal/carnivore/racoon.image
- IllegalArgumentException (RunTimeException)
- IllegalArgumentException (RunTimeException)
What needs to be kept on mind for indexing the elements in subpath?
- The root of the Path is not included in the subpath return value
- The directory next to root is the 0-th index element
What are Path symbols?
1) . -> refers to the current directory
2) .. -> refers to the Parent directory (one up directory)