獲取一個資料夾下所有指定字尾名(.java)的檔案(包括子資料夾中的內容),並將這些檔案的絕對路徑寫入到一個文字檔案中

fantasy-starry發表於2020-12-06
package com.Work3;

import org.junit.Test;

import java.io.*;

/**
 * @Author: 廾匸
 * @Date: 2020/12/2 19:34
 * @Description:
 * @version: 1.01
 */
public class Three {
    @Test
    public void test01(){
        try {
            show(new File("G:/project"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void show(File file) throws IOException {
        if(file.isFile()){
            if(file.getAbsolutePath().endsWith(".java")){
                return;
            }
        }
        File[] files = file.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if(pathname.isDirectory()||pathname.getAbsolutePath().endsWith(".java")){
                    return true;
                }
                return false;
            }
        });
        Writer writer = null;
        BufferedWriter bufferedWriter = null;
        for (File file1 : files) {
            if(file1.isFile()){
                writer = new FileWriter("G:/text.txt",true);
                bufferedWriter = new BufferedWriter(writer);
                bufferedWriter.write(file1.getAbsolutePath());
                bufferedWriter.newLine();
                bufferedWriter.flush();
            }else{
                show(file1);
            }
        }
    }
}

相關文章