Chapter 13. New I/O API (NIO.2)

NIO.2 was introduced with JDK 7 to provide enhanced file I/O support and access to the default filesystem. NIO.2 is supported by the java.nio.file and java.nio.file.attribute packages. The NIO.2 API is also known as JSR 203: More New I/O APIs for the Java Platform. Popular interfaces that are used from the API are Path, PathMatcher, FileVisitor, and WatchService. Popular classes that are used from the API are Paths and Files.

The Path Interface

The Path interface can be used to operate on file and directory paths. This class is an upgraded version of the java.io.File class. The following code demonstrates the use of some of the methods of the Path interface and the Paths class for acquiring information:

Path p = Paths.get("\opt\jpgTools\README.txt");
System.out.println(p.getParent()); // optjpgTools
System.out.println(p.getRoot()); // 
System.out.println(p.getNameCount()); // 3
System.out.println(p.getName(0)); // opt
System.out.println(p.getName(1)); // jpgTools
System.out.println(p.getFileName()); // README.txt
System.out.println(p.toString()); // The full path

The Path class also provides additional features, some of which are detailed in Table 13-1.

Table 13-1. Path interface capabilities
Path method Capability

path.toUri()

Converts a path to a URI object

path.resolve(Path)

Combines two paths together

path.relativize(Path)

Constructs a path from one location to another

path.compareTo(Path)

Compares two paths against each other

The Files Class

The Files class can be used to create, check, delete, copy, or move a file or directory. The following code demonstrates some commonly used methods of the Files class:

// Create directory
Path dirs = Paths.get("\opt\jpg\");
Files.createDirectories(dirs);
// Instantiate path objects
Path target1 = Paths.get("\opt\jpg\README1.txt");
Path p1 = Files.createFile(target1);
Path target2 = Paths.get("\opt\jpg\README2.txt");
Path p2 = Files.createFile(target2);
// Check file attributes
System.out.println(Files.isReadable(p1));
System.out.println(Files.isReadable(p2));
System.out.println(Files.isExecutable(p1));
System.out.println(Files.isSymbolicLink(p1));
System.out.println(Files.isWritable(p1));
System.out.println(Files.isHidden(p1));
System.out.println(Files.isSameFile(p1, p2));

// Delete, move, and copy examples
Files.delete(p2);
System.out.println(Files.move(p1, p2));
System.out.println(Files.copy(p2, p1));
Files.delete(p1);
Files.delete(p2);

The move method accepts the varargs enumeration using REPLACE_EXISTING or ATOMIC_MOVE. REPLACE_EXISTING moves the file, even if it already exists. ATOMIC_MOVE ensures that any process watching the directory will be able to access the complete file.

The copy method accepts the varargs enumeration with REPLACE_EXISTING, COPY_ATTRIBUTES, or NOFOLLOW_LINKS. REPLACE_EXISTING copies the file, even if it already exists. COPY_ATTRIBUTES copies the file attributes. NOFOLLOW_LINKS copies the links, but not the targets.

The lines, list, walk, and find methods have been added to the Files class relative to the Stream API. The lines method lazily reads a stream of lines. The list method lazily lists directory entries, and walk recursively traverses the entries. The find method lazily provides Path by searching for files in a file tree rooted at a given file node.

Additional Features

The NIO 2.0 API also provides the following features, which are good to know for the job. Questions about these features are also included on the Oracle Certified Professional Java SE 7 Programmer Exam. These items are not covered here, as they are more suited to a tutorial style guide or resource:

  • The ability to watch a directory using the WatchService interface.

  • The ability to recursively access directory trees using the FileVisitor interface.

  • The ability to find files using the PathMatcher functional interface.

Since PathMatcher is a functional interface, it may be used with a lambda expression:

PathMatcher matcher = (Path p) -> {
  // returns boolean
  return (p.toString().contains("World"));
};
Path path1 =  FileSystems.getDefault().getPath(
  "\tmp\Hello.java");
Path path2 =  FileSystems.getDefault().getPath(
  "\tmp\HelloWorld.java");
System.out.print("Matches: "
  + matcher.matches(path1) + ", "
  + matcher.matches(path2));

$ Matches: false, true
Tip

Consider using the new java.nio.file.DirectoryStream functional interface with the enhanced for loop to iterate over a directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.240.222