6 November 2013

Java 7 NIO.2 file handling (updated)

Java 7 contains a newer set of IO API's. This is an extension of the NIO (New Input/Output) API introduced in Java 1.4. The extenson is is called NIO.2 and specified in JSR 203. NIO.2 contains
  • a new API for handling filesystems and files
  • an API for asynchronous  I/O on both sockets and files
  • additional features on sockets ( binding, multicast datagrams...)

java.nio.file

In this article we will discuss the file handling API. All the classes for this functionality are in the new java.nio.file package.

The Path class

This is the new class  representing a file location (replacing corresponding methodsd in java.io.File).
The Paths (plural!) class contains static utility methods that return a Path object:
import java.nio.file.Path;
import java.nio.file.Paths; 


// p1: relative Path to a file
Path p1 = Paths.get(“subdir/in.txt"); 
// p2: Absolute path to c:\java\local\jan\Hello.java
// Paths.get() can take a variable number of arguments
// each extra argument indicates an extra directly level
Path p2 = Paths.get("c:\\java\\local”,”rijkswatch”,”Hello.java"); 
In the above example, remark that when using backslashes as a directory separator (Microsoft style), they need to be doubled to escape their special meaning in java String.

FileSystem support

These methods will give you information on the different filesystems on the machine, their mount points, type, size, free space...

The picture shows the output of the code in DOS.

// once more the plural class (FileSystems) contains static utility methods
// that produce objects of the singular (FileSystem) class
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths; 

FileSystem fs = FileSystems.getDefault(); 
for (Path rootPath : fs.getRootDirectories()) { 
  try { 
    FileStore store = Files.getFileStore(rootPath);
    System.out.println(rootPath + ": " + store.type()); 
  } catch (IOException e) { 
    System.out.println(rootPath + ": " + 
      "<error getting store details>"); 
  } 
} 


java.nio.file.Files methods

 

Static utilities in the Files class operate on Path (directory of file) objects.

searching in a directory

for (Path file : Files.newDirectoryStream(Path base, String pattern)){
   …
}
The pattern can contain wildcards like:

* any string
** any string, going down in directories
? any single character
[a,0-9] a or a number
[!a-z] NOT a lower case character
{one,two} String one or two

To recurse into subdirectories use Files.walkFileTree.

Some more static Files methods

public static long size(Path path) // Returns size of the file
public static boolean isReadable(Path path)
public static boolean isSymbolicLink(Path path) // path a soft link? 
  // LinkOption below specifies how symbolic links should be handled 
public static Path createLink(Path link,Path existing) // create hard link
public static Object getAttribute(Path path,String attribute,LinkOption... options)  
  // attribute "unix:nlink" gives # hard links in unix attribute space<
public static boolean exists(Path path, LinkOption... options)
public static boolean isDirectory(Path path, LinkOption... options)
public static Path move(Path source, Path target, CopyOption... options)
public static List<String> readAllLines(Path path, Charset cs) 

Switching between the new nio Path and the old io File

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

Path p1 = Paths.get(“subdir/in.txt");
File f1 = p1.getFile();
Path p2 = f2.getPath();

No comments:

Post a Comment