`
guyunduzai
  • 浏览: 16865 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

将一个文件夹下的多个文件合并到一个文件中

阅读更多
public class CombinationToOneFile {

    public static void main(String[] args) throws IOException, ParseException {

        //将所有文件合并到target.csv中

        FileOutputStream afterFos = new FileOutputStream("d:/combination/before12/"
                + "target.csv");
        OutputStreamWriter osw = new OutputStreamWriter(afterFos, "utf8");
        BufferedWriter out= new BufferedWriter(osw);

        //读取要合并的文件所在的文件夹

        File file = new File("D:\\before12");
        String test[];

        //读取该文件夹下面所有要合并的文件
        test = file.list();
        for (int i = 0; i < test.length; i++) {

            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    "d:/before12/" + test[i]), "utf8"));
            String r = br.readLine();
            while (r != null) {
                out.write(r);
                out.newLine();
                out.flush();
                r = br.readLine();
            }
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics