讀取檔案夾下的子檔案夾的所有檔案名稱,並儲存成以子檔案命名的txt檔案__java

來源:互聯網
上載者:User

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格式


輸入:


輸出:





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.