java 将文件夹所有的文件合并到指定的文件夹下

场景:将文件夹所有的文件合并到指定的文件夹下

代码:

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MergeAllFile {

    /**
     * 遍历某一个路径的所有文件,包括子文件夹
     * @param path
     * @return
     */
    public static List<File> getAllFilesAndDir(String path){
        File root = new File(path);
        List<File> files = new ArrayList<File>();
        if(!root.isDirectory()){
            files.add(root);
        }else{
            File[] subFiles = root.listFiles();
            for(File f : subFiles){
                files.addAll(getAllFilesAndDir(f.getAbsolutePath()));
            }    
        }
        return files;
    }
    
    /**
     * 遍历某一个路径的所有文件,不包括子文件夹
     * @param path
     * @return
     */
    public static List<File> getAllFiles(String path){
        File root = new File(path);
        //文件路径是文件夹,获取文件夹下的所有文件
        if(root.isDirectory()){
            return Arrays.asList(root.listFiles());
        }
        return new ArrayList<File>();
    }
    
    /**
     * 
     * @param listFiles 遍历得到的某路径下面所有的文件对象
     * @param destFullPath 合并的文件的全路径,包括文件名
     * @return
     */
    public static boolean MergeFiles(List<File> listFiles,String destFullPath){
        boolean flag = true;
        File destFile = new File(destFullPath);//获取到合并的文件对象
        BufferedOutputStream bos = null;
        BufferedReader br = null;
        try {
            //打开与目标文件对象的通道
            bos = new BufferedOutputStream(new FileOutputStream(destFile));
            long fileSize = 0;
            long limitSize = 67000;
            //遍历得到的某路径下面所有的文件对象
            for (File srcFile : listFiles) {
                //涉及合并文件,最好限制同名文件(根据需求判断是否需要)
                if((srcFile.getName()).equals(destFile.getName())){
                    System.out.println(srcFile.getName()+"文件不合法,不进行合并!");
                    continue;//跳过
                }
                
                //判断合并文件的格式
                if(srcFile.isFile() && srcFile.getName().endsWith(".dat")){
                    System.out.println(srcFile.getName()+"是合法文件,开始合并");
                    try {
                        //打开与源文件对象的通道
                        br = new BufferedReader(new FileReader(srcFile));
                        String line = null;
                        //循环读文件
                        while((line = br.readLine()) != null){
                            fileSize+=line.length();
                            if(fileSize > limitSize){
                                System.out.println("写入文件的大小,已超过限制【"+ limitSize +"】,大小已达到【"+ fileSize +"】");
                            }
                            line = line + System.getProperty("line.separator");//换行
                            bos.write(line.getBytes("UTF-8"));
                            bos.flush();
                        }
                    } finally {
                        if(br != null){
                            br.close();
                        }
                    }
                }else{
                    System.out.println(srcFile.getName()+"文件不满足合并格式!");
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            flag = false;
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
        } finally {
            try {
                if(bos != null){
                    bos.close();
                }
                if(br != null){
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }
    
    //测试
    public static void main(String[] args) {
        //定义需要复制文件的路径
        String srcPath = "E:\home\detail\20200120";
        String destName = "TEST_0001.dat";
        String destFullPath = "E:\home\detail\20200120\"+destName;
        List<File> files = getAllFiles(srcPath); 
        boolean flag = MergeFiles(files,destFullPath);
        System.out.println("合并完成的标记:"+flag);
    }
}
原文地址:https://www.cnblogs.com/liangxiaojin/p/12469104.html