The java.nio.file.SimpleFileVisitor Class is used to visit all the files in a directory and to re-throw I/O exceptions whenever an I/O error occurs.
Class declaration :
public class SimpleFileVisitor<T> extends Object implements FileVisitor<T>
Constructor:
- protected SimpleFileVIsitor(): Creates a new object of SimpleFileVisitor class.
Method:
Method |
Description |
---|---|
public FileVisitResult preVisitDirectory(T directory, BasicFileAttributes attributes) |
This method is invoked for this directory before the entries are visited. This method returns to CONTINUE unless overridden. |
public FileVisitResult postVisitDirectory(T directory, IOException e) |
This method is invoked for this directory after the entries are visited. This method returns to CONTINUE unless overridden. |
public FileVisitResult visitFile(T file, BasicFileAttributes attributes) |
This method is invoked for this file before in a directory. This method returns to CONTINUE unless overridden. |
public FileVisitResult visitFileFailed(T file, IOException exception) |
This method is invoked for a file that could not be visited. This method re-throws the I/O exception unless overridden. This was the exception that prevented the file from being visited. |
1.public FileVisitResult preVisitDirectory(T directory, BasicFileAttributes attributes): This method is invoked for this directory before the entries in the are visited. This method returns to CONTINUE unless overridden.
Parameters: directory- reference to this directory. attributes- attributes of this directory. Returns: the file visit result. Throws: I/O Exception.
2.public FileVisitResult postVisitDirectory(T directory, IOException e): This method is invoked for this directory after the entries in the are visited. This method returns to CONTINUE unless overridden.
Parameters: directory- reference to this directory. e- NULL if there is no error in this directory's iteration,else the I/O exception. Returns: the file visit result. Throws: I/O Exception.
3.public FileVisitResult visitFile(T file, BasicFileAttributes attributes): This method is invoked for this file before in a directory. This method returns to CONTINUE unless overridden.
Parameters: file- reference to this file. attributes- attributes of this file. Returns: the file visit result. Throws: I/O Exception
4.public FileVisitResult visitFileFailed(T file, IOException exception): This method is invoked for a file that could not be visited. This method re-throws the I/O exception unless overridden. This was the exception that prevented the file from being visited.
Parameters: file - reference to this file. exc - exception that prevented the file from being visited. Returns: the file visit result. Throws: I/O Exception
Java
// Java program to demonstrate working of simpleFileVisitor // class import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.*; public class FileVisitorDemo extends SimpleFileVisitor<Path> { public FileVisitResult visitFile(Path file, BasicFileAttributes atts) throws IOException { if (file.getFileName().toString().endsWith( ".txt" )) { // delete files ending with .txt Files.delete(file); } // return result of the operation return FileVisitResult.CONTINUE; } // Method to print message if file visit was not // successful public FileVisitResult visitFileFailed(Path file, IOException e) throws IOException { System.err.println( "File could not be visited" ); return FileVisitResult.CONTINUE; } public static void main(String args[]) throws Exception { FileVisitorDemo visitor = new FileVisitorDemo(); try { // visiting all files at // "/Users/abhinavjain194/desktop/visit" Files.walkFileTree( Paths.get( "/Users/abhinavjain194/desktop/visit" ), visitor); } catch (Exception e) { // printing error if occurred System.err.print(e.toString()); } } } |
Before running the program
After running the program