编写java代码将文件内容清空

package com.kaka.test;


import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class CleanFile {

    /**
     * 获取所有的文件和文件夹
     * @param filepath 路径
     */
    public static  List getAllFile(String filepath){
        List allFiles=new ArrayList<>();
        findFolder(new File(filepath),allFiles);
        return allFiles;
    }
    /**
     * 递归
     * @param file
     * @param allFiles
     */
    private static void findFolder(File file,List allFiles){
        if(file.isDirectory()){
            allFiles.add(file);
            File[] files= file.listFiles();
            for(File f:files){
                findFolder(f,allFiles);
            }
        }else{
            allFiles.add(file);
        }
    }

    public static void main(String[]  args) throws Exception {
        getPath();
    }

    public static void getPath() throws Exception {
        List files= CleanFile.getAllFile("C:\\Users\\Administrator\\Desktop\\OA相关流程");
        for(File f:files){
            test(f.getAbsolutePath());
        }
    }

    public static void test(String path) throws Exception{
        try{
        FileWriter writer = new FileWriter(path);
        writer.write("");
        writer.close();
        new File(path).delete();
    }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}

你可能感兴趣的:(java,IO流,FileWriter)