Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use File class application in java to traverse all files under a folder

Shulou Source: shulou.com Published: 2022-06-02 13:06:16 09月10日 Update

This article mainly introduces "java how to use File class application traversal folder all files", in daily operation, I believe many people in java how to use File class application traversal folder all files on the problem there are doubts, small make up all kinds of information, sort out simple and easy to use operation method, hope to answer "java how to use File class application traversal folder all files" doubts helpful! Next, please follow the small series to learn together!

Code:

package Travels all files in a folder;import java.io.File;public class Test { public static void main(String[] args){ File file=new File("D:\\tcb\\Week Summary"); filesDirs(file); } //Travels files in folders and subfolders using recursion public static void filesDirs(File file){ //File object is the path to a file or folder, the first level determines whether the path is empty if(file!= null){ //The second layer path is not empty, determine whether it is a folder or a file if (file.isDirectory()){ //Enter the description here as a folder. At this time, you need to get all the files under the current folder, including the directory File[] files=file.listFiles();//Note: Here you can only use listFiles(), you cannot use list() //All the contents under files, may be folders, may also be files, then you need to determine one by one whether it is a file or a folder. This determination process is the method encapsulated here.//Therefore, you can call yourself to determine, implement recursion for (File flies2:files) { filesDirs(flies2); } }else{ System.out.println("file name"+file); } }else{ System.out.println("file does not exist"); } }}

At this point, the study on "how to use File class application to traverse all files under folder in java" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Files folders applications learning paths methods more recursive help practical next layer 2 code content names objects that is articles theory Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi MySQL Apple vpn