百里登风

导航

通过java 来实现对多个文件的内容合并到一个文件中

现在有多个txt文本文件,需要把这么多个文件的内容都放到一个文件中去

 

以下是实现代码

package com.SBgong.test;
import java.io.*;

public class FileCombine {
    public static  void  main(String[] args) throws IOException {
       //定义输出目录
        String FileOut="E:\\Mycode\\SBgong\\output\\1.txt";
        BufferedWriter bw=new BufferedWriter(new FileWriter(FileOut));

      //读取目录下的每个文件或者文件夹,并读取文件的内容写到目标文字中去
        File[] list = new File("E:\\Mycode\\SBgong\\input\\2012-09-22").listFiles();
        int fileCount = 0;
        int folderConut= 0;
        for(File file : list)
        {
            if(file.isFile())
            {
                fileCount++;
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;
                while((line=br.readLine())!=null) {
                    bw.write(line);
                    bw.newLine();
                }
                br.close();
            }else {
                folderConut++;
            }
        }
        bw.close();
        System.out.println("输入目录下文件个数为"+fileCount);
        System.out.println("输入目录下文件夹个数为"+folderConut);

    }

}

 

运行结果:

 

 

 

posted on 2019-05-04 22:24  百里登峰  阅读(18251)  评论(0编辑  收藏  举报