Java method for reading all folders and file paths under it
Java reads the paths of all folders and files under it. The details are as follows
If in the F: \ AA folder, there are folders and files as shown in the following figure:
Then, in Java, read all file paths under F: / AA as follows:
1. Firstly, because the ArrayList under the file and container class is used, the following packages should be introduced at the beginning:
2. The method is as follows. File dirfile can accept all legal paths except the drive letter, such as F:. Access is denied because there are some system files under the drive letter. Null pointer exceptions may occur when reading the drive letter.
The above method is not to read xlsx under the new folder under F: \ AA. If a folder needs to read all its subfolders and files at the same time during reading, recursion must be used. First, set a global dynamic array:
Then the method is as follows:
In fact, in the process of reading, the key is to use the listfiles() method to obtain the list of all files in this folder, Then, like [Java] move folder and all its sub files and sub folders (click the open link), and [Java] complete the operation of copying all files in one folder to another folder by using the file input / output stream (click the open link), recursion is performed in case of a folder.