In this chapter we discuss the Traverse folder.
1. Traverse the root directory of the project through recursive traversal
Package Com.ray.ch16;import Java.io.file;public class Test {public static void Showallfiles (String path) {File root = new File (path); file[] files = root.listfiles (); for (File file:files) {if (File.isdirectory ()) {Showallfiles (File.getabsolutepath ());} else if (File.isfile ()) {System.out.println (File.getabsolutepath ());}}} public static void Main (string[] args) {showallfiles (".");}}
Output:
(Due to more, no longer output)
2. Increase the control of the name on the above basis
Package Com.ray.ch16;import Java.io.file;import Java.util.regex.pattern;public class Test {public static void Showallfiles (string path, string regex) {File root = new File (path); file[] files = root.listfiles (); for (File file:files) {if (File.isdirectory ()) {Showallfiles (File.getabsolutepath (), RE Gex);} else if (File.isfile ()) {Pattern pattern = pattern.compile (regex); if (Pattern.matcher (File.getname ()). Matches ()) { System.out.println (File.getabsolutepath ());}}} public static void Main (string[] args) {showallfiles ("F:\\ws101\\thinkinjava", "[A-za-z]+.java");}}
Output:
(Due to more, no longer output)
The above code controls only output Java source files.
Summary: This chapter focuses on two examples of traversing a folder.
This chapter is here, thank you.
-----------------------------------
Directory
Learn from the java-16.2 Traverse folder