按顺序合并txt小说

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Xiaoshuo2 {
    public static ArrayList<String> keys = new ArrayList<String>();
    public static ArrayList<String> values = new ArrayList<String>();
    public static List<String> fileNames = new ArrayList<>();


    //创建目录
    static {
        //创建新目录
        File dirr = new File("./aaaaa");
        if(dirr.exists()){

        }else {
            dirr.mkdir();
        }
        //获取原目录所有文件名,存到list集合里面
        File file = new File("./d");
        File[] files = file.listFiles();
        for (File f : files) {
            fileNames.add(f.getName());
        }
        add("第一章","0001");
        add("第二章","0002");
        add("第三章","0003");
        add("第四章","0004");
        add("第五章","0005");
        add("第六章","0006");
        add("第七章","0007");
        add("第八章","0008");
        add("第九章","0009");
        add("第十章","0010");







    }
    //读取原文件
    public static BufferedReader br(String str)  {
        try {
            FileInputStream fis = new FileInputStream(str);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            return br;
        } catch (IOException e){
            return null;
        }

    }

    //分别给俩list集合添加key,value
    public static void add(String key,String value) {
        keys.add(key);
        values.add(value);
    }
    //获取value
    public static String getValue(String key) {
        return values.get(keys.indexOf(key));
    }




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

        for(String f : fileNames) {
            Path path = Paths.get("./d/" + f); //原目录文件路径
            BufferedReader br = br("./d/" + f);

            String line1 = br.readLine();
            //第二行
            String line2 = br.readLine();
            String regex = "第.*章";

            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(line2);



            if(m.find()) {

                String ss = getValue(m.group());
                System.out.println(ss);
                String path2 = "./aaaaa/"+ss+".txt";
                File fil = new File(path2);
                if (fil.createNewFile()){
                    System.out.println("创建成功");
                }else{
                    System.out.println("已存在");
                }

                List<String> lines = Files.readAllLines(path);
                BufferedWriter bw = new BufferedWriter(new FileWriter(path2));
                for (int i = 0; i < lines.size();i++) {
                    bw.write(lines.get(i).toString());
                    bw.newLine();
                    bw.flush();
                }
                bw.close();

            }



        }








    }
}

最后使用bat命令
copy *.txt xxxx.txt
合并整理好顺序的多个txt文件为一个txt文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简 洁 冬冬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值