Java 17 Recipes
-8. Iterating Over Files in a Directory
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- How It Works
8-8. Iterating Over Files in a Directory
Problem You need to scan files from a directory. There are possibly subdirectories with more files. You want to include those in your scan. Solution Using the NIO.2, creates a java.nio.file .FileVisitor the desired implementation within its visitFile method. Next, obtain the default FileSystem object and grab a reference to the path you’d like to scan via the getPath() method. Lastly, invoke the Files.walkFileTree() method, passing the Path and the FileVisitor these tasks. public class Ch_8_8_TraverseDirectory { private void start() { FileVisitor myFileVisitor = new SimpleFileVisitor () { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.println("Visited File: "+file.toString()); return FileVisitResult.CONTINUE; } }; FileSystem fileSystem = FileSystems.getDefault(); Path directory= fileSystem.getPath("."); try { Files.walkFileTree(directory, myFileVisitor); } catch (IOException e) { e.printStackTrace(); } } ChApTer 8 InpuT And OuTpuT 316 public static void main (String args[]) { Ch_8_8_TraverseDirectory traverseDirectory = new Ch_8_8_ TraverseDirectory(); traverseDirectory.start(); } How It Works Before NIO.2, trying to traverse a directory tree involved recursion, and depending on the implementation, it could be very brittle. The calls to get files within a folder were synchronous. They required scanning the whole directory before returning, generating what would appear to be an unresponsive method call to an application user. With NIO.2, one can specify which folder to start traversing on, and the NIO.2 calls handle the recursion details. The only item you provide to the NIO.2 API is a class that tells it what to do when a file/folder is found ( java.nio.file .SimpleFileVisitor NIO.2 uses a Visitor pattern, so it isn’t required to prescan the entire folder but instead processes files as they are being iterated over. The implementation of the SimpleFileVisitor class includes overriding the visitFile(Path file, BasicFileAttributesattrs() method. When you override this method, you can specify the tasks to perform when a file is encountered. The visitFile method returns a FileVisitReturn enum. This enum then tells the FileVisitor • CONTINUE continues with the traversing of the directory tree. • TERMINATE stops the traversing. • SKIP_SUBTREE stops going deeper from the current tree level (useful only if this enum is returned on the preVisitDirectory() method). • SKIP_SIBLINGS skips the other directories at the same tree level as the current. The SimpleFileVisitor the following. • preVisitDirectory is called before entering a directory to be traversed. • postVisitDirectory is called after finished traversing a directory. ChApTer 8 InpuT And OuTpuT 317 • visitFile is called as it visits the file, as in the example code. • visitFileFailed is called if the file cannot be visited; for example, on an I/O error. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling