In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of traversing file tree of JDK7's new features, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
Sometimes you need to recursively traverse a file tree, such as finding qualified files in a folder, looking for files created one day. The jdk7 nio package provides a new interface, FileVisitor. It provides various operations to traverse the file tree.
PreVisitDirectory-called when a path is accessed
PostVisitDirectory-all nodes of a path are called after they are accessed. If an error occurs, exception will pass it to this method
VisitFile-called when the file is accessed. The file properties of the file are passed to this method
VisitFileFailed-this method is called when the file cannot be accessed. Exception is passed to this method.
If you are lazy, you don't want to implement all the methods. You can choose to inherit SimpleFileVisitor. It helps you to implement the above methods, you only need to Override the methods you are interested in.
Here's an example: simply iterate through a folder and print out all the information
Java code
Import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes / * * @ author kencs@foxmail.com * / public class FileVisitorTest extends SimpleFileVisitor {private void find (Path path) {System.out.printf ("access -% s:%s%n", (Files.isDirectory (path)? " Directory ": file"), path.getFileName ();} @ Override public FileVisitResult visitFile (Path file,BasicFileAttributes attrs) {find (file); return FileVisitResult.CONTINUE;} @ Override public FileVisitResult preVisitDirectory (Path dir,BasicFileAttributes attrs) {find (dir); return FileVisitResult.CONTINUE } @ Override public FileVisitResult visitFileFailed (Path file,IOException e) {System.out.println (e); return FileVisitResult.CONTINUE;} public static void main (String [] args) throws IOException {if (args.roomthroomroom1) {System.out.println ("Please enter a file path as a parameter") System.exit (- 1);} Files.walkFileTree (Paths.get (args [0]), new FileVisitorTest ());}}
Randomly select a path as a parameter
Java FileVisitorTest "C:\ Program Files\\ Java\\ jre7\\ bin"
Run the result code
Access-directory: bin access-file: awt.dll access-file: axbridge.dll access-directory: client access-file: classes.jsa access-file: jvm.dll access-file: Xusage.txt access-file: dcpr.dll access-file: deploy.dll access-file: deployJava1.dll access-file: dt_shmem.dll access Q-document: dt_socket.dll.
Note that there are four types of FileVisitResult
CONTINUE.-continue.
TERMINATE-terminated, this traversal is over
SKIP_SUBTREE-the subtree (a subdirectory of the current path) is no longer traversed
SKIP_SIBLINGS-sibling nodes (directories of the same level) are no longer accessed.
You can control the process of traversing the file tree through these return values
The above is all the content of the article "sample Analysis of traversing the File Tree of JDK7's New Features". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.