|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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 :
|
#4
| |||
| |||
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
| |||
| |||
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); |
#6
| |||
| |||
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 :
|
![]() |
|
Tags: file tree, filevisitor, filevisitresult, java, walkfiletree, walking |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |