java traverse folder and all subfolders

When I used to write code loops through folders and subfolders, always write your own recursive access, the study lucene today, we have to find JDK provides access to traverse, on the code:

. 1 String STR = "C: \\ LLY the Users \\ \\ \\ Desktop New Folder" ;
 2          the Path = Files.walkFileTree path (Paths.get (STR), new new SimpleFileVisitor <the Path> () {
 . 3  
. 4              / * * 
5               * performed before access to the folder
 . 6               * @param the dir
 . 7               * @param attrs
 . 8               * @return 
. 9               * @throws IOException
 10               * / 
. 11              @Override
 12 is              public FileVisitResult preVisitDirectory (the Path the dir, BasicFileAttributes attrs)throws IOException {
13                 return super.preVisitDirectory(dir, attrs);
14             }
15 
16             /**
17              * 访问文件
18              * @param file
19              * @param attrs
20              * @return
21              * @throws IOException
22              */
23             @Override
24             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
25                 return super.visitFile(file, attrs);
26             }
27 
28             /**
29              * 访问失败执行
30              * @param file
31              * @param exc
32              * @return
33              * @throws IOException
34              */
35             @Override
36             public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
37                 return super.visitFileFailed(file, exc);
38              }
 39  
40              / * 
41               * all files in the folder executed immediately after Q
 42 is               * @param the dir
 43 is               * @param EXC
 44 is               * @return 
45               * @throws IOException
 46 is               * / 
47              @Override
 48              public FileVisitResult postVisitDirectory (the Path the dir , IOException EXC) throws IOException {
 49                  return  Super .postVisitDirectory (the dir, EXC);
 50              }
 51 is         });

 

Guess you like

Origin www.cnblogs.com/lly001/p/10958695.html