Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2010
Member
 
Join Date: Jul 2006
Posts: 273
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.!!
__________________
(\__/)
(='.'=) This is Bunny. Copy and paste bunny into your
(")_(") signatureto help him gain world domination
Reply With Quote
  #2  
Old 19-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
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.
Reply With Quote
  #3  
Old 19-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
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 :
  1. preVisitDirectory(T) - This method is invoked before a directory's entries are visited.
  2. preVisitDirectoryFailed(T, IOException) - This method is invoked when the directory can not be visited.
  3. visitFile - This method is invoked on the file being visited.
  4. visitFileFailed - This method is invoked when the file cannot be accessed.
  5. postVisitDirectory - This method is invoked after all the entries in a directory are visited.
Reply With Quote
  #4  
Old 19-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
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;
    }
}
Reply With Quote
  #5  
Old 19-02-2010
Member
 
Join Date: Nov 2008
Posts: 996
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.
Reply With Quote
  #6  
Old 19-02-2010
Member
 
Join Date: Nov 2008
Posts: 1,192
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 :
  1. Continue - This method indicates that the file walking should continue.
  2. Terminate - Using this method will immediately abort the file walking.
  3. Skip_Siblings - When preVisitDirectory returns this value, the specified directory is not visited, postVisitDirectory is not invoked, and no further unvisited siblings are visited.
  4. Skip_Subtree - When preVisitDirectory returns this value, the specified directory and its subdirectories are skipped.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Walking the File Tree in Java"
Thread Thread Starter Forum Replies Last Post
How to get file tree configuration into the Windows Explorer of Windows 7? Pawna Operating Systems 5 30-04-2011 10:34 AM
How to use a Tree Expansion Listener in Java? Rob Dizzle Software Development 4 13-02-2010 06:03 AM
JAVA binary tree Daren Software Development 4 29-09-2009 12:17 AM
avl tree java adjust height Hakon Software Development 2 04-06-2009 12:34 PM


All times are GMT +5.5. The time now is 05:56 AM.