Results 1 to 6 of 6

Thread: Walking the File Tree in Java

  1. #1
    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

  2. #2
    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.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  4. #4
    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;
        }
    }

  5. #5
    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.

  6. #6
    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.

Similar Threads

  1. Replies: 5
    Last Post: 30-04-2011, 10:34 AM
  2. How to use a Tree Expansion Listener in Java?
    By Rob Dizzle in forum Software Development
    Replies: 4
    Last Post: 13-02-2010, 06:03 AM
  3. JAVA binary tree
    By Daren in forum Software Development
    Replies: 4
    Last Post: 29-09-2009, 12:17 AM
  4. avl tree java adjust height
    By Hakon in forum Software Development
    Replies: 2
    Last Post: 04-06-2009, 12:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,883,728.93258 seconds with 17 queries