zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    TensorFlow学习笔记(四)——TensorFlow运作方式入门、可视化
    Tensorflow学习笔记(三)——深入MNIST
    Tensorflow学习笔记(二)——MNIST机器学习入门
    TensorFlow学习笔记(一)
    【HTML打卡】0125 实战首页布局之导航
    【HTML打卡】0123 HTML语义标签
    【HTML打卡】0119css 文字、图片、控制器、引入方式、初始化
    面试题目总结
    kafka学习知识点总结(三)
    kafka学习知识点总结(二)
  • 原文地址:https://www.cnblogs.com/liangxiaojin/p/12469104.html
Copyright © 2011-2022 走看看