[JAVA]怎麼列出未知層數的文件夾的全部文件的路徑?放文件夾超過1000層時怎麼列出?
想起某個垃圾在裏面回了這句話,剛好我我又解決這個問題了,直接用小號上答案
/*
* 編寫軟件:NETBEANS
*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package 操作文件夾;
import java.io.File;
import java.util.ArrayList;
/**
*
* @author Administrator
*/
public class 操作文件夾 {
public static ArrayList<String> 獲取文件夾下的全部文件_含多層子目錄的文件名(String 文件路徑) {
ArrayList<String> 文件名_ArrayList = new ArrayList<String>();
File 文件路徑_Directory = new File(文件路徑);
if (!文件路徑_Directory.isDirectory()) {
return 文件名_ArrayList;
}
File[] 子文件或目錄集合 = 文件路徑_Directory.listFiles();
for (File 子文件或目錄 : 子文件或目錄集合) {
if (子文件或目錄.isDirectory()) {
文件名_ArrayList.addAll(操作文件夾.獲取文件夾下的全部文件_含多層子目錄的文件名(子文件或目錄.getPath()));
} else {
文件名_ArrayList.add(子文件或目錄.getName());
}
}
return 文件名_ArrayList;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ArrayList<String> 獲取文件夾下的全部子文件的文件名 = 獲取文件夾下的全部文件_含多層子目錄的文件名("C:\\Users\\Administrator\\Desktop\\AccessPort137");
for (int i = 0; i < 獲取文件夾下的全部子文件的文件名.size(); i++) {
System.out.println(獲取文件夾下的全部子文件的文件名.get(i));
}
}
}