Here we'll see two examples - Copy operation & Traversing a file tree. If you have already worked using JDk 6 or earlier versions then you can see how easy it is to use JDK 7's nio package. And you need to write less code to implement the same.

This tutorial shows two examples on the file IO mechanism introduced in the JDK 7. The java.nio.file package provides support for file IO and for accessing the default file system. This package provides very intuitive and easy to use file handling API. And it also reduces number of lines of code required to implement file handling related application functionalities.

Technologies used in this article

  1. JDK 7
  2. Eclipse 3.7

For our example, we'll use file operations supported by java.nio.file.Files. This class contains many static methods (like copy, move, delete, walkFileTree etc.) that operate on files / directories. And these methods mostly delegate to the associated file system provider to perform the file operations.

java.nio.file.Path is one of the primary entrypoints of the java.nio.file package. Path class represents a path in the underlying file system

Example 1 : File Copy operation

To copy from source file to the target file 'Files.copy' method is used. We need to provide different options using 'java.nio.file.CopyOption' to specify how the copy should be done.

File : CopyOPDemo.java

package com.srccodes.file;
 
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
 
/**
 * 
 * @author Abhijit Ghosh
 * @version 1.0
 */
public class CopyOPDemo {
 
    public static void main(String[] args) throws IOException {
        // File to be copied
        Path srcFile = Paths.get("C:/srccodes/srcFile.txt");
         
        // Copied file
        Path targetFile = Paths.get("C:/srccodes/destFile.txt");
         
        // configure how to copy or move a file.
        CopyOption[] options = new CopyOption[] {StandardCopyOption.REPLACE_EXISTING};
         
        // Copy srcFile to targetFile
        Files.copy(srcFile, targetFile, options);
    }
 
}

Example 2 : Traverse a file tree

In this example we'll traverse a file tree using 'Files.walkFileTree' method. This method requires root / starting path and an implementation of 'SimpleFileVisitor' class where we have overridden two methods 'preVisitDirectory' and 'visitFile' to define what we want to do during visit of a directory and file. To make it simple we have only printed directory and file name.

File : RecursiveFileListDemo.java

package com.srccodes.file;
 
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
 
/**
 * 
 * @author Abhijit Ghosh
 * @version 1.0
 */
public class RecursiveFileListDemo {
 
    public static void main(String[] args) throws IOException {
        Path startPath = Paths.get("C:/srccodes");
 
        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println(">>>>Dir : " + dir);
                return FileVisitResult.CONTINUE;
            }
 
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("File : " + file);
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

Console Output

File Tree to be traversed
File Tree
console

>>>>Dir : C:\srccodes
File : C:\srccodes\destFile.txt
>>>>Dir : C:\srccodes\dir1
File : C:\srccodes\dir1\doc1.doc
File : C:\srccodes\dir1\zip1.zip
File : C:\srccodes\srcFile.txt

Download SrcCodes

All code samples shown in this post are available in the following link JDK7FileOP.zip.

References