Walk File Tree with Java NIO

Last modified on July 23rd, 2015 by Joe.

This Java NIO tutorial is to learn about walking a file tree. In a file handling application walking a file tree will be a regular requirement. Java NIO has given FileVisitor interface using which we can walk through a file tree. This tutorial is part of the Java NIO tutorial series.

Walk with NIO FileVisitor

FileVisitor is an interface and we need to implement it to walk a file tree. FileVisitor has got the following four methods,

SimpleFileVisitor Class:

SimpleFileVisitor is a class with default implementation to visit all files and on error it will re-throw errors. Instead of implementing FileVisitor we can choose to extend SimpleFileVisitor and override only methods of our need.

Initiate File Walk

Once the FileVisitor is implemented we can use the NIO util class Files to start the File walk. Files class has got the following two methods,

tree

Key Points about FileVisitor:

File Tree Walk Flow Control

We have good control over the file tree navigation. Every method returns FileVisitResult and we can set the value to control the further action on the tree navigation using it. FileVisitResult has the following values,

Following example demonstrates the usage of above option. When a file named data.log is located, then the file walk is terminated.

@Override
public FileVisitResult postVisitDirectory(Path path, IOException ioe)
		throws IOException {
	Path destinationPath = Paths.get("data.log");

	if (path.getFileName().equals(destinationPath)) {
		System.out.println("File found: " + path);
		return FileVisitResult.TERMINATE;
	}
	return FileVisitResult.CONTINUE;
}

NIO File Visitor Example

Following file visitor just walks through the file tree starting from the path passed and prints the type of file. It CONTINUEs till the end of file tree.

package com.javapapers.java.nio;

import static java.nio.file.FileVisitResult.CONTINUE;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class SimpleFileVisitorExample extends SimpleFileVisitor<Path> {

	@Override
	public FileVisitResult visitFile(Path path,
			BasicFileAttributes basicFileAttributes) {

		if (basicFileAttributes.isRegularFile()) {
			System.out.println(path + " is a regular file with size "
					+ basicFileAttributes.size());
		} else if (basicFileAttributes.isSymbolicLink()) {
			System.out.println(path + " is a symbolic link.");
		} else {
			System.out.println(path
					+ " is not a regular file or symbolic link.");
		}
		return CONTINUE;
	}

	@Override
	public FileVisitResult postVisitDirectory(Path path, IOException ioException) {
		System.out.println(path + " visited.");
		return CONTINUE;
	}

	@Override
	public FileVisitResult visitFileFailed(Path path, IOException ioException) {
		System.err.println(ioException);
		return CONTINUE;
	}
}

Comments on "Walk File Tree with Java NIO"

  1. arna326466 says:

    Perhaps you want to walk the file tree looking for a particular directory and, when found, you want the process to terminate. Perhaps you want to skip specific directories.

Comments are closed for "Walk File Tree with Java NIO".