java 遞迴呼叫自動讀取某資料夾下所有檔案(遞迴呼叫)
阿新 • • 發佈:2019-01-05
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; public class readFile { private static ArrayList<String> listname = new ArrayList<String>(); public static void main(String[] args)throws Exception{ readAllFile("data/"); System.out.println(listname.size()); } public static void readAllFile(String filepath) { File file= new File(filepath); if(!file.isDirectory()){ listname.add(file.getName()); }else if(file.isDirectory()){ System.out.println("檔案"); String[] filelist=file.list(); for(int i = 0;i<filelist.length;i++){ File readfile = new File(filepath); if (!readfile.isDirectory()) { listname.add(readfile.getName()); } else if (readfile.isDirectory()) { readAllFile(filepath + "\\" + filelist[i]);//遞迴 } } } for(int i = 0;i<listname.size();i++){ System.out.println(listname.get(i)); } } }