Wednesday, July 3, 2024
HomeLanguagesJavajava.nio.file.SimpleFileVisitor Class in Java

java.nio.file.SimpleFileVisitor Class in Java

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments