Over a million developers have joined DZone.

How to Traverse a File Tree in NIO.2 and Java 7 (JDK 7)

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

JDK 7 and NIO.2 brings new and great I/O features – many of them are related with file system and files. Based on these features I tried to develop a simple Search File application. The idea is to provide a file name and the application will detect all system roots and search for that file – it is also a good idea to specify a starting folder with a few intuitive modifications.
Well, to accomplish this task we need two things:

java.nio.file.FileVisitor interface - has hooks for before, during, and  after a file is "visited", as well as for when failure occurs.

walkFileTree method (new in NIO.2) which can be use to traverse a tree of directories and files. When you invoke this method, you specify the root and how  many levels deep you want to go. You can also set an attribute to indicate that it should follow links.

Here is the Search File solution:
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

class Search implements FileVisitor {

private final Path searchedFile;
public boolean found = false;

public Search(Path searchedFile) {
this.searchedFile = searchedFile;
}

boolean search(Path file) throws IOException{
if (file.getFileName().equals(searchedFile)) {
System.out.println("Searched file was found: " + searchedFile
+ " in " + file.toRealPath(true).toString());
found = true;
return true;
}

return false;
}

@Override
public FileVisitResult postVisitDirectory(Object dir, IOException exc)
throws IOException {
System.out.println("Visited: " + (Path) dir);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)
throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)
throws IOException {
boolean success = search((Path) file);
if (!success) {
return FileVisitResult.CONTINUE;
} else {
return FileVisitResult.TERMINATE;
}
}

@Override
public FileVisitResult visitFileFailed(Object file, IOException exc)
throws IOException {
return FileVisitResult.CONTINUE;
}
}

class Main {

public static void main(String[] args) throws IOException {

Path searchDir = null;
File[] roots = File.listRoots();
Path searchFile = Paths.get("rafa.png"); //this is the searched file
Search go = new Search(searchFile);
EnumSet opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);

for (File root : roots) {
if (!go.found) {
searchDir = Paths.get(root.getPath());
Files.walkFileTree(searchDir, opts, Integer.MAX_VALUE, go);
}
}

if (!go.found) {
System.out.println("The file " + searchFile + " was not found!");
}
}
}


 

From http://e-blog-java.blogspot.com/2011/03/how-to-traverse-file-tree-in-nio2-and.html

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}