MENU

Java recursively searches for files

TOC

Java directoryfrom (e.g. time, place, numerical quantity)recursiveSearch for files in

Java introduces a sample program that recursively searches for files in a specified directory, including subdirectories, and obtains a list of files that match the search criteria.
They are equivalent to the UNIX command ls -R or the Windows command dir /s.
You can also specify * as a wildcard character in the file name to be searched, or use a regular expression to search.

 As an example of usage, the following recursively searches the directory filelist, including subdirectories, to obtain a list of files with the extension java.

FileSearch search = new FileSearch();
File[] files = search.listFiles("C:/filelist/", "*.java");


sample program

Execution Result

 To check the sample program, we created a filelist directory and prepared the following files.

The file name and update date are displayed.

The current date is2007/08/18Suppose it is.

C:\filelist
    │ aaa.java 2007/08/18
    │ bbb.java 2007/08/18
    └─dir
           ccc.java 2007/07/17
           ddd.java 2007/08/18
           eee.jpg 2007/08/16

◆Example of Execution


◆Output result

Retrieve all files
1: C:\filelist\aaa.java
2: C:\filelist\bbb.java
3:C:\filelist\dir\ccc.java
4: C:\filelist\dir\ddd.java
5: C:\filelist\dir\dir\eee.jpg

Obtaining a file with the extension java
1: C:\filelist\aaa.java
2: C:\filelist\bbb.java
3: C:\filelist\dir\ccc.java
4: C:\filelist\dir\ddd.java

Get all files and directories
1: C:\filelist\aaa.java
2: C:\filelist\bbb.java
3: C:\filelist\dir
4: C:\filelist\dir\ccc.java
5: C:\filelist\dir\ddd.java
6: C:\filelist\dir\dir\eee.jpg

Obtaining files that have been updated since two days ago from the current date
1: C:\filelist\aaa.java
2: C:\filelist\bbb.java
3: C:\filelist\dir\ddd.java
4: C:\filelist\dir\dir\eee.jpg

Retrieve files older than 30 days from the current date
1: C:\filelist\dir\ccc.java
TOC