18 Eylül 2019 Çarşamba

SimpleFileVisitor Sınıfı

Giriş
Açıklaması şöyle.
A simple visitor of files with default behavior to visit all files and to re-throw I/O errors.
Methods in this class may be overridden subject to their general contract.
Bu sınıf bize default boş metodlar tanımladığı için tüm metodlarını override etmek zorunda değiliz. Açıklaması şöyle.
A SimpleFileVisitor implements each method of FileVisitor. Each implemented method returns a FileVisitResult that tells the walk algorithm to continue traversing the tree; that is what “visit all files” in the class javadoc means. Any IOExceptions are propagated, rather than caught and suppressed; that is what “re-throw I/O errors” in the class javadoc means.

The “simple” part means that a SimpleFileVisitor has no functionality of its own. It’s an “empty” class: each method does nothing other than allowing the file tree traversal to continue.

Finally, the class javadoc states the intended purpose of the class: subclasses are expected to override one or more methods. As with all object-oriented inheritance, the subclass is expected to adhere to the written contract (javadoc) of each method, as well as that of each inherited class and interface.
previsitDirectory metodu
Şöyle yaparız.
@Override
public FileVisitResult preVisitDirectory(Path dir,
              BasicFileAttributes attrs) throws IOException {
  dir.toString(); //İşlenen dizin
  ...
  return FileVisitResult.CONTINUE;
}
visitFile metodu
Şöyle yaparız.
@Override
public FileVisitResult visitFile(Path file,
              BasicFileAttributes attrs) throws IOException {
  ...
  return FileVisitResult.CONTINUE;
}
Eğer arama dursun istersek şunu döneriz.
return FileVisitResult.TERMINATE;
İşlenen dosyanın yolu şöyle alınır.
file.getFileName()




Hiç yorum yok:

Yorum Gönder