Java reads the paths of all folders and files below. The specific content is as follows
If in the f:/aa folder, there are folders and files as shown below:
Then, in Java, all file paths under f:/aa are read in this way:
1. First of all, since ArrayList is used under the file and container classes, the following packages should be introduced at the beginning:
import java.io.*; import java.util.*;
2. The method is as follows, where File dirFile can accept all legal paths except for the drive letter, such as f:. Because the drive letter contains some system files, access is denied, because the drive letter is read, a null pointer exception may occur.
//Here we only query all folders and files in the current path and store their paths to the file array // Since we encounter a folder that does not query it contains all subfolders and files, there is no need to use recursive public static ArrayList<String> Dir(File dirFile) throws Exception { ArrayList<String> dirStrArr = new ArrayList<String>(); if (dirFile.exists()) { //Turn directly take out all folders and files in the current path using listFiles() to store all folders and files in the current path into a file array File files[] = dirFile.listFiles(); for (File file : files) { //If the passed parameter dirFile is ended with a file separator, that is, / or /, then construct it like this if (dirFile.getPath().endsWith(File.separator)) { dirStrArr.add(dirFile.getPath() + file.getName()); } else { // Otherwise, if there is no file separator, add a file separator and add the file name to the path dirStrArr.add(dirFile.getPath() + File.separator + file.getName()); } } return dirStrArr; } The method on it is to not read xlsx in the newly created folder under f:/aa. If during the reading process, when a folder wants to read all subfolders and files, recursion is used and a global dynamic array is set first:
public static ArrayList<String> dirAllStrArr = new ArrayList<String>();
Then the method is as follows:
public static void DirAll(File dirFile) throws Exception { if (dirFile.exists()) { File files[] = dirFile.listFiles(); for (File file : files) { //If a folder is encountered, it will be called recursively. if (file.isDirectory()) { // Recursively call DirAll(file); } else { // If you encounter a folder, put it in the array if (dirFile.getPath().endsWith(File.separator)) { dirAllStrArr.add(dirFile.getPath() + file.getName()); } else { dirAllStrArr.add(dirFile.getPath() + File.separator + file.getName()); } } } } } } } } } } } } } } } } } } } } } } In fact, during the reading process, the key is to use the listFiles() method to obtain the list of all files in this folder. Then, like "[Java] Move folders and all subfiles and subfolders" (click to open the link) and "[Java] uses file input and output streams to complete the operation of copying all files in one folder to another folder" (click to open the link), and then recursively encountering folders.
The whole method above is a java file like this:
import java.io.*; import java.util.*; public class fileList { // Set a global dynamic array to store the file path // When traversing folders and containing all subfolders and files, recursion is used, so you should set public static ArrayList<String> dirAllStrArr = new ArrayList<String>(); // Here is to query all folders and files under the current path and store their paths to the file array // Since the folder does not query it contains all subfolders and files, there is no need to use recursion public static ArrayList<String> Dir(File dirFile) throws Exception { ArrayList<String> dirStrArr = new ArrayList<String>(); if (dirFile.exists()) { // Directly take out all folders and files in the current path using listFiles() to store all folders and files in a file array File files[] = dirFile.listFiles(); for (File file : files) { // If the passed parameter dirFile is ended with a file separator, that is, / or /, then construct it like this if (dirFile.getPath().endsWith(File.separator)) { dirStrArr.add(dirFile.getPath() + file.getName()); } else { // Otherwise, if there is no file separator, add a file separator and add the file name to the path dirStrArr.add(dirFile.getPath() + File.separator + file.getName()); } } } return dirStrArr; } public static void DirAll(File dirFile) throws Exception { if (dirFile.exists()) { File files[] = dirFile.listFiles(); for (File file : files) { // If a folder is encountered, it will be called recursively. if (file.isDirectory()) { // Recursively call DirAll(file); } else { // If a folder is encountered, put it in an array if (dirFile.getPath().endsWith(File.separator)) { dirAllStrArr.add(dirFile.getPath() + file.getName()); } else { dirAllStrArr.add(dirFile.getPath() + File.separator + file.getName()); } } } } public static void main(String[] args) throws Exception { File dirFile = new File("f:/aa"); System.out.println(Dir(dirFile)); DirAll(dirFile); System.out.println(dirAllStrArr); } }The operation results are as follows:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.