Walking the File Tree in Java
Hi Friends,
You really did the great work by providing me the coding of the necessary thing. Extremely thanks for that. Now I am having another doubt of Core Java. I want to know about the walking the file tree. I thought that instead of searching on web, you guys will provide more needful information. So please tell me about the Walking the File Tree in Java. Also try to reply as soon as possible.!!:notworthy
Re: Walking the File Tree in Java
In some scenarios you need to create an application that will recursively visit all the files in a file tree. For doing that you will have to delete every .class file in a tree. Otherwise you will have to find every file that hasn't been accessed from long time. If you want to do this, you will have to use the FileVisitor interface. To walk a file tree, you first need to implement a FileVisitor. You can try coding by creating your own class that extends the Walker class.
Re: Walking the File Tree in Java
The first thing is implementing a a FileVisitor while walking a file tree. The required behavior at key points in the traversal process is specified by a FileVisitor. The behavior like when a file is visited, directory is accessed, when a failure occurs is specified. The FileVisitor Interface has five methods :
- preVisitDirectory(T) - This method is invoked before a directory's entries are visited.
- preVisitDirectoryFailed(T, IOException) - This method is invoked when the directory can not be visited.
- visitFile - This method is invoked on the file being visited.
- visitFileFailed - This method is invoked when the file cannot be accessed.
- postVisitDirectory - This method is invoked after all the entries in a directory are visited.
Re: Walking the File Tree in Java
I have provided you with an example that extends FileVisitor to print all entries in a file tree. It prints the entry whether the entry is a regular file. The following is the code that explains the same :
Code:
import static java.nio.file.FileVisitResult.*;
public static class PrintFilesDemo extends FileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes ba) {
if (ba.isSymbolicLink()) {
System.out.format("Symbolic link: %s ", file);
} else if (ba.isRegularFile()) {
System.out.format("Regular file: %s ", file);
} else {
System.out.format("Other: %s ", file);
}
System.out.println("(" + ba.size() + "bytes)");
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.format("Directory: %s%n", dir);
return CONTINUE;
}
@Override
public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
System.err.println(exc);
return CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.err.println(exc);
return CONTINUE;
}
}
Re: Walking the File Tree in Java
After implementing your FileVisitor, there are two walkFileTree methods in the Files class. You can invoke the PrintFiles file visitor as follows :
Code:
Path startingDir = ...;
PrintFiles prntf = new PrintFiles();
Files.walkFileTree(startingDir, prntf);
Hope that you got the point how to use the walkFileTree method.
Re: Walking the File Tree in Java
Sometimes it happens that you want to walk the file tree looking for a particular directory and, when found, you want the process to terminate. You should also know that the FileVisitor methods return a FileVisitResult value. If you want, you can abort the file walking process or control whether a directory is visited by the values you return in the FileVisitor methods by the following way :
- Continue - This method indicates that the file walking should continue.
- Terminate - Using this method will immediately abort the file walking.
- Skip_Siblings - When preVisitDirectory returns this value, the specified directory is not visited, postVisitDirectory is not invoked, and no further unvisited siblings are visited.
- Skip_Subtree - When preVisitDirectory returns this value, the specified directory and its subdirectories are skipped.