Learn from the java-16.2 Traverse folder

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.