按行拆分文本文件与合并文本文件---I/O流---java

背景


  • 在进行自然语言处理的中文词性标注时   进行测试时由于测试数据文本行数较多  而且测试每次标注一行的用时稍长
  • 如果一次将文件读进来测试机器运行时间要连续不能中断  而且 只能一台机器进行工作 
  • 于是想到分布式的方式  将大文件拆分成小文件  分别用于测试  再将得到的结果文件 合并成 一个大文件 用于评估

拆分文本文件


  • 在此文件的文件夹进入powershell   使用如下命令进行拆分
//             源文件  目标文件  拆分的个数
java splitfile fromfile tofiles subfilenumber
  •  详细代码
public class splitfile {

    public static void main(String[] args) throws Exception{
        DoFile dofile = new DoFile();
        // 源文件  目标文件  拆分的个数
        //dofile.splitfile("test.dat", "subtest/test", 10);
        dofile.splitfile(args[0], args[1], Integer.parseInt(args[2]));
        
    }

}
public class DoFile {
    
    public int getrows(File file) throws IOException{
        int rows = 0;
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(file)));
        for (rows = 0; br.readLine() != null; rows++);
        br.close();
        return rows;
    }
    
    public void splitfile(String from, String to, int subfilenum) throws IOException{
        File fromfile = new File(from);
        
        if (!fromfile.exists()) {
            throw new IOException(fromfile + "不存在!!!");
        }
        if (!fromfile.isFile()) {
            throw new IOException(fromfile + "不是文件!!");
        }
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(fromfile)));
        
        String str = null;
        int filerows = getrows(fromfile);
        int subrows = (int) Math.ceil(1.0 * filerows / subfilenum);

        for (int i = 0; i < subfilenum; i++) {

            PrintWriter pw = new PrintWriter(to + "-" + i + ".dat");
            int row = 0;
            System.out.println("生成第:" + (i+1) + "个文件");
            while(row < subrows) {
                
                if ((str = br.readLine()) != null) {
                    pw.println(str);
                    row++;
                }else {
                    pw.flush();
                    pw.close();
                    break;
                }
                
            }
            pw.flush();
            pw.close();
        }

        br.close();
        System.out.println("文件拆分完成!");
    }
}

  • 测试结果


合并文本文件


  • 在此文件的文件夹进入powershell   使用命令
//装待合并的文件的文件夹  生成的新的文件
java mergefile dir newfile
  •  详细代码
public class mergefile {

    public static void main(String[] args) throws Exception{
        DoFile dofile = new DoFile();
        
        //合并文件  装小文件的文件夹   大文件
        dofile.mergefile(args[0], args[1]);
    }

}
public class DoFile {
    
    //按行   合并文件    给定已知文件夹
     public void mergefile(String dirname, String finalfile) throws IOException{
        File dir = new File(dirname);
        if (!dir.exists()) {
            throw new IOException(dir + "不存在!!");
        }
        if (!dir.isDirectory()) {
            throw new IOException(dir + "不是文件夹");
        }
        FileOutputStream out = new FileOutputStream(finalfile, true);
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(out));
        int i = 1;
        String[] files = dir.list();
        
        for(String string: files) {
            System.out.println("合并到第:" + i++ + "个文件");
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            new FileInputStream(dir + "/"+ string)));
            String str = null;
            while((str = br.readLine()) != null) {
                bw.write(str);
                bw.newLine();
            }
            br.close();
        }
        bw.flush();
        bw.close();
        System.out.println("文件合并完成!");
            
    }   
    
}

  • 测试结果

原文地址:https://www.cnblogs.com/xinglichao/p/8985789.html