import java.io.File; import java.io.FileNotFoundException;import java.io.FileWriter;import java.io.IOException; public class TakeFilePathAndName { public static void main(String[] args) throws FileNotFoundException{ // This is the path where the file's name you want to take. String path = "D:\\10stanford 100"; File file = new File(path); File[] array = file.listFiles(); String s = null; for(int i =0;i< array.length;i++) { try { s = "d:\\stanford\\"+array[i].getName()+".txt"; File file1 = new File(s); file1.createNewFile(); } catch (IOException e) { // TODO 自動產生的 catch 塊 e.printStackTrace(); } getFile(array[i].getPath(),s); //得到根目錄下的檔案路徑 } } private static void getFile(String path,String s) { // get file list where the path has // get the folder list File file1 = new File(path); File[] array = file1.listFiles(); try { FileWriter output = new FileWriter(s); for(int i=0;i<array.length;i++){ if(array[i].isFile()) { //System.out.println(array[i].getName()); output.append(array[i].getName());//添加進去檔案(圖片)名字 } }output.close(); } catch (IOException e) {// TODO 自動產生的 catch 塊e.printStackTrace();} } }
因為一些需要,需要讀取檔案夾下的子檔案夾的所有檔案名稱,並儲存成以子檔案為名稱的檔案txt格式
輸入:
輸出: