6. Java 7: File NIO2 Flashcards
How would you write the following code to point to a file path using Java 7 new features? File file = new File("readme.txt");
Path path = Paths.get(“readme.txt”);
Given the following code, how can a File be converted to a Path object? File file = new File("readme.txt");
file.toPath();
Given the following code what will be printed?
Path path = Paths.get(“//home/user/jordi”);
System.out.println(path.toString());
/home/user/jordi
Paths.get or FileSystem.getDefault().getPath will preform minor syntatic cleanup on paths.
Given the following file path how would one loop over the directories and print them?
/home/user/jordi/readme.txt
Path path = Paths.get(“/home/user/jordi/readme.txt”);
for(int i = 0; i < path.getNameCount; i++) {
System.out.println(path.getName(i));
}
Given the following path how would you print the filename?
Path path = Paths.get(“//home/user/jordi/readme.txt”);
path.getFileName();
What is the result of subPath?
Path path = Paths.get(“//home/user/jordi/readme.txt”);
Path subPath = path.subPath(0,2);
/home/user
What is the result of root?
Path path = Paths.get(“./user/jordi”);
Path root = path.getRoot();
null
path.getRoot(); is only usefull when using absolute paths. For example if the path (on windows) would have been C:\users it would have returned C:\
Given the following how would one replace jordi with bar?
Path path = Paths.get(“/user/jordi”);
path.resolveSibling(“bar”);
Given the following code, what is printed?
Path path = Paths.get(“/user/jordi/../../foo”);
path.normalize();
System.out.println(path.getNameCount());
System.out.println(path.toString());
5
/foo
Normalize will remove redundant paths from the complete path. However the getNameCount(); will still return the orignal number of Name parts.
What is the result of the following: Path p1 = Paths.get("home"); Path p2 = Paths.get("home/users/jordi"); Path relative_1_2 = p1.relativize(p2); Path relative_2_1 = p2.relativize(p1);
users/jordi
../..
How would one move into a sub-directory using a path?
path.resolve(“subdir”);
What is the Files class used for?
Class with static methods that uses Path objects to work with files and folders.
What are the available options when using Files to do a copy operation?
LinkOption.NOFOLLOW_LINK
Indicates that if the file is symbolic link it should copy the link and not the file linked to.
StandardCopyOption.COPY_ATTRIBUTES
Copies the originals file attributes to the new file, the attributes are platform independent but the last-modified-time is supported accross platform.
StandardCopyOption.REPLACE_EXISTING
Performs the copy operation even if the target file already exists.If the target is a non-empty directory the copy fails with FileAlreadyExistsException.
How to copy a file from /home/readme.txt to /home/readme2.txt using the FIles class? The last modified time should also be copied to the new file and existing files should be overwritten.
Path p1 = Paths.get(“/home/readme.txt”);
Path p2 = Paths.get(“/home/readme2.txt”);
Files.copy(p1, p2, StandardCopyOption.COPY_ATTRIBUTES,
StandardCopyOption.REPLACE_EXISTING
);
What are the available options when using Files to do a move operation?
StandardCopyOption.REPLACE_EXISTING
Performs the copy operation even if the target file already exists.If the target is a non-empty directory the copy fails with FileAlreadyExistsException.
StandardCopyOption.ATOMIC_MOVE
It will move the file into a directory any process watching the directory will only access the file when it’s completly moved (not partially). Throws a exception when a ATOMIC_MOVE is not supported by the filesystem.