Java loops all folders and file codes under a folder

Source: Internet
Author: User

Given a folder in the actual project, there is not much need to get all the files in this folder, more to find or delete a specific file


Import Java.io.File;
Import java.util.ArrayList;

public class Filetest {
private static arraylist<string> filelist = new arraylist<string> ();

public static void Main (string[] args) throws Exception {

String FilePath = "D:/xinxi";
GetFiles (FilePath);
}
/*
* Recursively get all the directories and their files under a certain path
*/
static void GetFiles (String filePath) {
File root = new file (FilePath);
file[] files = root.listfiles ();
for (File file:files) {
if (File.isdirectory ()) {
/*
* Recursive call
*/
GetFiles (File.getabsolutepath ());
Filelist.add (File.getabsolutepath ());
System.out.println ("Show" +filepath+ "all subdirectories and their files" +file.getabsolutepath ());
}else{
System.out.println ("Show" +filepath+ "All Subdirectories" +file.getabsolutepath ());
}
}
}
}

1. Method calls that do not use recursion.

public void TraverseFolder1 (String path) {
int filenum = 0, foldernum = 0;
File File = new file (path);
if (file.exists ()) {
linkedlist<file> list = new linkedlist<file> ();
file[] files = file.listfiles ();
for (File file2:files) {
if (File2.isdirectory ()) {
System.out.println ("folder:" + File2.getabsolutepath ());
List.add (file2);
filenum++;
} else {
System.out.println ("File:" + File2.getabsolutepath ());
foldernum++;
}
}
File Temp_file;
while (!list.isempty ()) {
Temp_file = List.removefirst ();
Files = Temp_file.listfiles ();
for (File file2:files) {
if (File2.isdirectory ()) {
System.out.println ("folder:" + File2.getabsolutepath ());
List.add (file2);
filenum++;
} else {
System.out.println ("File:" + File2.getabsolutepath ());
foldernum++;
}
}
}
} else {
SYSTEM.OUT.PRINTLN ("file does not exist!");
}
System.out.println ("folder total:" + Foldernum +), a total of files: "+ FileNum);

}

Called using recursive method.

public void TraverseFolder2 (String path) {

File File = new file (path);
if (file.exists ()) {
file[] files = file.listfiles ();
if (Files.length = = 0) {
System.out.println ("folder is empty!");
Return
} else {
for (File file2:files) {
if (File2.isdirectory ()) {
System.out.println ("folder:" + File2.getabsolutepath ());
TraverseFolder2 (File2.getabsolutepath ());
} else {
System.out.println ("File:" + File2.getabsolutepath ());
}
}
}
} else {
SYSTEM.OUT.PRINTLN ("file does not exist!");
}
}

Related Article

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.