我是靠谱客的博主 甜美大白,最近开发中收集的这篇文章主要介绍java合并文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class OutPutFile {
    public static void main(String[] args) throws IOException {
        List<String> list2 = new ArrayList();
        list2.add("aa|table2|张一");
        list2.add("bb|table3|张二");
        list2.add("cc|table1|张三");
        list2.add("dd|table4|张四");
        list2.add("ee");
        list2.add("ff");
        list2.add("gg");
        String outTable = "F:/AAA/test2/table/successTable.txt";
        // 合并
        getFiles(list2, outTable);
    }
    public static void getFiles(List<String> path, String outTable) throws IOException {
        List<String> listOutSP = new ArrayList();
        List<String> listOutTable = new ArrayList();
        List<String> listOutTableName = new ArrayList();
        for (String excelPath : path){
            String[] str = excelPath.split("\|");
            if (str.length > 1){
                String spPath = "F:/AAA/test1/SP/" + str[0] + ".txt";
                listOutSP.add(spPath);
                String tablePath = "F:/AAA/test1/table/" + str[1] + ".txt";
                listOutTable.add(tablePath);
                String tableName = str[2];
                listOutTableName.add(tableName);
            }else {
                String spPath = "F:/AAA/test1/SP/" + str[0] + ".txt";
                listOutSP.add(spPath);
            }
        }
        int page = 1;
        int end = listOutSP.size();
        int pageSize = 2;
        while (end > 0){
            end = end - page*pageSize;
            List<String> listOutSP2 = new ArrayList();
            listOutSP2 = listOutSP.subList((page-1)*pageSize, page*pageSize);
            String out = "F:/AAA/test2/SP/" + String.valueOf(page)+".txt";
            page += 1;
            mergeFileToSP(listOutSP2,out);
        }
        List<String> listOutSP3 = new ArrayList();
        listOutSP3 = listOutSP.subList((page-1)*pageSize, listOutSP.size());
        String out3 = "F:/AAA/test2/SP/" + String.valueOf(page)+".txt";
        mergeFileToSP(listOutSP3,out3);
        mergeFileToTable(listOutTable, listOutTableName, outTable);
    }

    public static void mergeFileToSP(List<String> files, String outPath) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(outPath));
        for (int i = 0; i < files.size(); i++) {
            File file = new File(files.get(i));
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
            bufferedReader.close();
        }
        bw.close();
    }

    public static void mergeFileToTable(List<String> files, List<String> listOutTableName, String outTable) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(outTable));
        for (int i = 0; i < files.size(); i++) {
            File file = new File(files.get(i));
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
            bw.write("------" + listOutTableName.get(i));
            bw.newLine();
            bufferedReader.close();
        }
        bw.close();
    }
}

最后

以上就是甜美大白为你收集整理的java合并文件的全部内容,希望文章能够帮你解决java合并文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部