Java method for reading all the folders and file paths under it, java path
Java reads the path of all the folders and files under it. The specific content is as follows:
If the f: \ aa folder contains folders and files:
In Java, all file paths under f:/aa are read as follows:
1. Because ArrayList under the file and container class is used, the following package should be introduced at the beginning:
import java.io.*; import java.util.*;
2. The method is as follows. In addition to the drive letter, such as f:, File dirFile can accept all valid paths. Because the drive letter contains some system files, access is denied. Because the drive letter is read, a null pointer exception may occur.
// Query all the folders and files in the current path and store the files in the file array. // If a folder contains all subfolders and files, therefore, recursive public static ArrayList <String> Dir (File dirFile) throws Exception {ArrayList <String> dirStrArr = new ArrayList <String> (); if (dirFile. exists () {// use listFiles () to store all folders and files in the current path to a File array File files [] = dirFile. listFiles (); for (File file: files) {// if the passed dirFile parameter ends with a File separator, that is, // or \, the if (dirFile. getPath (). endsWith (File. separator) {dirStrArr. add (dirFile. getPath () + file. getName ();} else {// otherwise, if there is no file separator, a file separator is added and the file name is added to the path dirStrArr. add (dirFile. getPath () + File. separator + file. getName () ;}}return dirStrArr ;}
The above method does not read xlsx in the new folder under f: \ aa, when a folder needs to read all its subfolders and files at the same time, recursion is required. First, a global dynamic array is set:
public static ArrayList<String> dirAllStrArr = new ArrayList<String>();
The method is as follows:
Public static void DirAll (File dirFile) throws Exception {if (dirFile. exists () {File files [] = dirFile. listFiles (); for (File file: files) {// recursively calls a folder. If (file. isDirectory () {// recursively call DirAll (file);} else {// put the folder into 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, in the process of reading, the key is to use the listFiles () method to obtain the list of all files in this folder, and [Java] mobile folders and all their subfolders and subfolders (click to open the link ), [Java] uses the file input and output stream to copy all files in a folder to another folder (click to open the link). recursion is performed when a folder is encountered.
The above method is like a java file:
Import java. io. *; import java. util. *; public class fileList {// sets a global dynamic array to store the file path // mainly traverses the folder. recursion is used when all subfolders and files are contained, therefore, set public static ArrayList <String> dirAllStrArr = new ArrayList <String> (); // query all the folders and files in the current path and store the files in the file array. // If a folder contains all subfolders and files, therefore, recursive public static ArrayList <String> Dir (File dirFile) throws Exception {ArrayList <String> dirStrArr = new ArrayList <String> (); if (dirF Ile. exists () {// use listFiles () to store all folders and files in the current path to a File array File files [] = dirFile. listFiles (); for (File file: files) {// if the passed dirFile parameter ends with a File separator, that is, // or \, the if (dirFile. getPath (). endsWith (File. separator) {dirStrArr. add (dirFile. getPath () + file. getName ();} else {// otherwise, if there is no file separator, a file separator is added and the file name is added to the path dirStrArr. add (dirFile. getPath () + File. separator + file. getName () ;}} return dirS TrArr;} public static void DirAll (File dirFile) throws Exception {if (dirFile. exists () {File files [] = dirFile. listFiles (); for (File file: files) {// recursively calls a folder. If (file. isDirectory () {// recursively call DirAll (file);} else {// put the folder into the 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 running result is as follows:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.