Flashcards in NIO.2 Deck (77)
Which is the entry point for working with NIO.2?
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?
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
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?
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 can we get the file separator using Path?
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?
What is the other way of accessing the Path object without Paths.get()?
Path path = FileSystems.getDefault().getPath(URI);
Path path = FileSystems.getDefault().getPath(String, String...)
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);
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();
which methods represents a String representation of a entir path?
Which is the only method in the Path interface that returns a string?
What is the output of followinig code:
Path path = Paths.get("C:\\Dilli\\Dilli.txt");
for(int i=0; i
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.
1. What is the getName(int) index starts from? 0-based or 1-based?
What type is the getName(int) method returns?
Which method returns the FileName of the Path? What type does it returns>
It returns a Path object
returns the Parent path or null if there is no parent.
parent of tiger.txt is /zoo/hello
parent of hello is /zoo
parent of zoo is /
1. 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?
1. 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?
1. It returns an another path object which is the copy of the given absolute path
What is the output of this when runs in Windows?
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));
3. IllegalArgumentException (RunTimeException)
4. IllegalArgumentException (RunTimeException)
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