English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
There are two ways to traverse all files in a folder in Java:
1Recursive traversal is usually the first method that developers can think of, the advantages of recursive traversal are: relatively simple implementation, less code, and higher execution efficiency, but the disadvantages are: relatively high memory consumption, and higher hardware requirements
// Recursive traversal private void getDirectory(File file) { File flist[] = file.listFiles(); if (flist == null || flist.length == 0) { return 0; } for (File f : flist) { if (f.isDirectory()) { //All folders will be listed here System.out.println("Dir==>" + f.getAbsolutePath()); getDirectory(f); } else { //All files will be listed here System.out.println("file==>" + f.getAbsolutePath()); } } }
2Non-recursive traversal is relatively easy to understand, but non-recursive traversal may not be easy to come up with at first, I also struggled for a long time, and found that Java has a LinkedList thing, which is also roughly known, it is used to save a list of linked files, if there is one, it is convenient to deal with it, the specific idea is like this: when traversing a folder, if it is a folder, it is added to the LinkedList, and if it is a file, it is listed; this way, all files and folders in the directory are traversed, and all folders are saved to the LinkedList; so the remaining task is to traverse the files in the folders in the LinkedList, the traversal method is the same as the above operation, if it is a folder, it is added to the LinkedList (Ps: the LinkedList in the traversal process is always the same list), of course, after taking a folder from the list, it needs to be deleted from the list. Here, LinkedList.removeFirst() is used to read, which means reading the first element from the list and removing it from the list. Only when the state of the LinkedList is isEmty does it mean that the traversal is complete.
// Non-recursive traversal private void GetDirectorySize(File file) { LinkedList list = new LinkedList(); //Save the list of folders to be traversed GetOneDir(file, list); //Call the method to traverse files under the root directory of the folder File tmp; while (!list.isEmpty()) { tmp = (File) list.removeFirst(); //The judgment here is a bit redundant, but for safety, a judgment is still given, normally the list only contains folders //However, it does not exclude special cases, such as: the target itself is a folder, which has become a file after being pushed onto the stack if (tmp.isDirectory()) { GetOneDirSize(tmp, list); } else { System.out.println("file==>" + tmp.getAbsolutePath()); } } } // Traverse files under the root directory of the specified folder private void GetOneDir(File file , LinkedList list){ //This method is called every time a folder is traversed System.out.println("Dir==>" + f.getAbsolutePath()); File[] files = file.listFiles(); sumdir += 1; if (files == null || files.length == 0) { return ; } for (File f : files) { if (f.isDirectory()) { list.add(f); } else { //Here is a list of all files under the root directory of the current folder System.out.println("file==>" + f.getAbsolutePath()); } } }
Section 2: Traverse a folder using Java and get all its contents
package demo0;823; 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) {}} String filepath = "G:\\Test\\icon"; getFiles(filepath); } static void getFiles(String filepath){ File root = new File(filepath); File[] files = root.listFiles(); for (File file : files){ if(file.isDirectory()){ //If file is a directory, loop and recursively call the cabinet //Recursive call getFiles(file.getAbsolutePath()); fileList.add(file.getAbsolutePath()); System.out.println("Display"+filepath+"all subdirectories and their files"+file.getAbsolutePath()); } //if it is not a directory, traverse its underlying files System.out.println("Display"+filepath+"all subfiles"+file.getAbsolutePath()); } } } }
Illustration:
Summary
This is the complete code for traversing files under the Java background folder in this article. I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site. If there are any shortcomings, please leave a message to point them out. Thank you for your support to this site!
Declaration: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please report via email to codebox.com (replace # with @) and provide relevant evidence. Once verified, this site will immediately delete the infringing content.