18 Eylül 2019 Çarşamba

FileVisitor Arayüzü

Giriş
Şu satırı dahil ederiz.
import java.nio.file.FileVisitResult;
import static java.nio.file.FileVisitResult.*;
import java.nio.file.FileVisitor;
Açıklaması şöyle. Bu arayüz yerine SimpleFileVisitor kullanılabilir.
A FileVisitor is an object whose callback methods are invoked as a method such as Files.walkFileTree traverses an entire hierarchy of files under a common root directory. As each file or directory in the tree is encountered, we say it is “visited.” At that point, the FileVisitor argument that was passed to the walk method has one or more of its methods invoked, depending on the nature of each file.
Örnek
Şöyle yaparız.
Path path = ...;

Files.walkFileTree(path, new FileVisitor<Path>() {

  @Override
  public FileVisitResult preVisitDirectory(Path dir,
    BasicFileAttributes attrs) throws IOException {
    ...
    return CONTINUE;
  }

  @Override
  public FileVisitResult visitFile(Path file,
    BasicFileAttributes attrs) throws IOException {
    ...
    return CONTINUE;
  }

  @Override
  public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
    ...
    return CONTINUE;
  }

  @Override
  public FileVisitResult postVisitDirectory(Path dir,IOException exc) throws IOException {
    return CONTINUE;
  }
});

Hiç yorum yok:

Yorum Gönder