📜  Java的.nio.file.SimpleFileVisitor类在Java中

📅  最后修改于: 2022-05-13 01:55:40.319000             🧑  作者: Mango

Java的.nio.file.SimpleFileVisitor类在Java中

Java.nio.file.SimpleFileVisitor 类用于访问目录中的所有文件,并在发生 I/O 错误时重新抛出 I/O 异常。

类声明:

public class SimpleFileVisitor
extends Object
implements FileVisitor

构造函数:

  • protected SimpleFileVIsitor():创建一个 SimpleFileVisitor 类的新对象。

方法:

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):在访问 中的条目之前为此目录调用此方法。除非被覆盖,否则此方法将返回到 CONTINUE。

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):在访问了 中的条目后,为此目录调用此方法。除非被覆盖,否则此方法将返回到 CONTINUE。

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):在一个目录中之前为这个文件调用这个方法。除非被覆盖,否则此方法将返回到 CONTINUE。

Parameters:
file- reference to this file.
arributes- attributes of this file.

Returns: the file visit result.

Throws: I/O Exception 

4.public FileVisitResult visitFileFailed(T file, IOException exception):针对无法访问的文件调用此方法。除非被覆盖,否则此方法将重新引发 I/O 异常。这是阻止访问文件的异常。

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 {
    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 occured
            System.err.print(e.toString());
        }
    }
}


运行程序前

运行程序后