Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

DZone's Guide to

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

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}