Working with paths and files in Java 7

Well, lots happening in the latest java upgrade and ironically most of us are comfortable with Java 5 or 6. Enough said.

The changes in NIO is quite impressive. Find below the quick reference for paths. The new Classes are found in the java.nio.file package. Lets see though the most common and useful entry points.

  • java.nio.file.Files;
  • java.nio.file.Path;
  • java.nio.file.Paths;

Starting with Path,

The Path class includes various methods that can be used to obtain information about the path, access elements of the path, convert the path to other forms, or extract portions of a path. Let’s see how to create a path

// Microsoft Windows syntax
Path path = Paths.get("C:\\home\\joe\\foo");

// Solaris syntax
Path path = Paths.get("/home/joe/foo");

Path contains all sorts of useful methods to get information about the file system. For instance, the relativize method that can be used to construct a relative path between two paths. Paths can be compared, and tested against each other using the startsWith and endWith methods.

See the example for a Windows file system

Path path = Paths.get("C:\\Temp\\data\\git-new.png");
System.out.format("The path is %s%n", path.toString());
System.out.format("The path's parent is %s%n", path.getParent().toString());
System.out.format("The path's root is %s%n", path.getRoot().toString());

Path relativeRoot = Paths.get("C:\\Temp\\");

System.out.format("%nRelatice root is is %s%n", relativeRoot.toString());
Path relativePath = relativeRoot.relativize(path);

System.out.format("%nRelative path is %s%n", relativePath.toString());

And the output

The path is C:\Temp\data\git-new.png
The parent of the path is C:\Temp\data
The root of the path is C:\

The relative root path is C:\Temp

The relative path is data\git-new.png

Moving onto Files,

This class consists exclusively of static methods that operate on files, directories, or other types of files. Files works like a breeze using Path.

Lets see a couple of uses

Reading and writing from/to a file 

Let us see two methods, one which reads from a file to a byte array and the another which writes to a file from a byte array. Both of them work with Paths.

// The below methods are for small files

byte[] readSmallBinaryFile(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
return Files.readAllBytes(path);

void writeSmallBinaryFile(byte[] aBytes, String aFileName) throws IOException {
Path path = Paths.get(aFileName);
Files.write(path, aBytes); //creates, overwrites

//In case of large files, use a BufferedWriter

Charset charset = Charset.forName("US-ASCII");
String s = "";
try (BufferedWriter writer = Files.newBufferedWriter(path, charset)) {
writer.write(s, 0, s.length());
} catch (IOException x) {
System.err.format("IOException: %s%n", x);

Most beautiful way to move files,

Moving files were a bit cumbersome, considering the amount of code needed for so trivial an operation, see here.

The Files class has a static method move,

public static Path move(Path source,
        Path target,
        CopyOption... options)
                 throws IOException

This takes in two path parameters, one for the source and one for the target. The third parameter being a list of CopyOptions. Right now, there are three Copy Options, and there could be more.

See the example below,

Path source = /var/tmp/source/out.log;
Path target = /vat/tmp/destination/out.log;

And the copy options are pretty cool,


Move the file as an atomic file system operation.

Copy attributes to the new file.

Replace an existing file if it exists.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s