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.
How to move a file from /home/readme.txt to /readme2.txt using the Files class? It should replace any already existing files.
Path p1 = Paths.get("/home/readme.txt"); Path p2 = Paths.get("/readme2.txt"); Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING );
What is the result of the following (given p1 and p2 are valid Path objects):
Files.move(p1, p2,
StandardCopyOption.COPY_ATTRIBUTES
);
throws UnsupportedOperaionException
COPY_ATTRIBUTES is not a valid option for move.
How to use the Files class to read all bytes from a file names /home/readme.txt?
Path p1 = Paths.get(“/home/readme.txt”);
byte[] bytes = Files.readAllBytes(p1);
Files class provides two methods for creating a BufferedReader and a BufferedWriter, name these two method signatures.
Files.createNewBufferedReader(Path path, Charset cs, OpenOption… options);
Files.createNewBufferedWriter(Path path, Charset cs, OpenOption… options);
How to create an InputStream using the Files class for file /home/readme.txt.
Path p1 = Paths.get("/home/readme.txt"); Files.newInputStream(p1);
The Files class methods sometimes take none or more OpenOptions. What are the StandardOpenOptions?
APPEND - When opened for WRITE, append new bytes to the end of the file.
CREATE - Create a new file if it doesn’t exist. Ignored if CREATE_NEW is also present
CREATE_NEW - Create a new file, fails if the file already exists.
DELETE_ON_CLOSE - When the JVM closes the file it will do a best effort to try and delete the file (not guaranteed).
DSYNC - Requires that every change of the data is synchronously written to the underlying storage device.
SYNC - Requires that every change of the date or metadat is synchronously written to the underlying storage device.
READ - Open for read access.
SPARSE - when used with CREATE_NEW then it will mark the file as a Sparse file (only possiible if the File system supports it).
TRUNCATE_EXISTING - if opened with WRITE access the file will be emptied on open.
WRITE - Open for write access.
How would one open a directory called /home and loop ofer it’s contents that end with .java?
Path p1 = Paths.get("/home"); Files.newDirectoryStream(p1, *.java);
Name the two methods for creating temp files using the Files class?
createTempFile(String prefix, String suffix, FileAttribute>… atrs);
- Create a temp file using the default directory. The prefix and suffix can be used to control the filename that is generated. For example if prefix is myapp_ and suffix is .myapp then the generated filename could be myapp_182391082930.myapp.
createTempFile(Path dir, String prefix, String suffix, FileAttribute>… atrs);
-Creates a temp file in the specified dir.
Describe the interface to recursively access a directory tree?
public interface FileVisitor { FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs) throws IOException; FileVisitResult visitFile(T file, Basic, BasicFileAttributes attrs) throws IOException; FileVisitResult visitFileFailed(T file, IOException e) throws IOException; FileVisitResult postVisitDirectory(T dir, IOException e) throws IOException; }