【问题标题】:Merge Two text files line by line using java使用java逐行合并两个文本文件
【发布时间】:2013-10-03 16:38:31
【问题描述】:

第一个文本文件 A.txt;

asdfghjklqw12345 qwe3456789
asdfghjklqw12345 qwe3456789

第二个文本文件 B.txt;

|记录 1:被拒绝 - 表 AUTHORIZATION_TBL 上的错误,列 AUTH_DATE.ORA-01843:无效月份| |记录 2:被拒绝 - 表 AUTHORIZATION_TBL 的错误,列 AUTH_DATE.ORA-01843:无效的月份|

第三个文本文件 C.txt;

asdfghjklqw12345 qwe3456789 |记录 1:被拒绝 - 表 AUTHORIZATION_TBL 上的错误,列 AUTH_DATE.ORA-01843:无效月份|

asdfghjklqw12345 qwe3456789 |记录 2:被拒绝 - 表 AUTHORIZATION_TBL 上的错误,列 AUTH_DATE.ORA-01843:不是有效月份|

对于上述情况,我想将两个不同文本文件中的两行合并为一行。我的代码如下

    List<FileInputStream> inputs = new ArrayList<FileInputStream>();
    File file1 = new File("C:/Users/dell/Desktop/Test/input1.txt");
    File file2 = new File("C:/Users/dell/Desktop/Test/Test.txt");

    FileInputStream fis1;
    FileInputStream fis2;

    try {
        fis1 = new FileInputStream(file1);
        fis2= new FileInputStream(file2);

        inputs.add(fis1);
        inputs.add(fis2);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    int total = (int) (file1.length() + file2.length());
    System.out.println("total length is " + total);

    SequenceInputStream sis = new                                                            SequenceInputStream(Collections.enumeration(inputs));
    try {
        System.out.println("SequenceInputStream.available() = "+ sis.available());

        byte[] merge = new byte[total];

        int soFar = 0;
        do {
            soFar += sis.read(merge,total - soFar, soFar);
        } while (soFar != total);
        DataOutputStream dos = new DataOutputStream(new        FileOutputStream("C:/Users/dell/Desktop/Test/C.txt"));
        soFar = 0;
        dos.write(merge, 0, merge.length);
        dos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

【问题讨论】:

  • 你想做什么?
  • 到目前为止你有没有尝试过?如果是,请告诉我们!
  • 你尝试过什么来实现它?
  • 1 2 3 4 5 是单独的行吗?您的规范与示例不符。也请分享您的代码。
  • 嗨,Thomas,请帮我找出解决上述问题的方法...紧急

标签: java merge dataoutputstream


【解决方案1】:

改进了 Masudul 的答案以避免编译错误:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class MergeText {
    public static void main(String[] args) throws IOException {
        StringBuilder output = new StringBuilder();
        try (Scanner sc1 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\A.txt")));
             Scanner sc2 = new Scanner((new File("C:\\Users\\YourName\\Desktop\\B.txt")))) {

            while (sc1.hasNext() || sc2.hasNext()) {
                String s1 = (sc1.hasNext() ? sc1.next() : "");
                String s2 = (sc2.hasNext() ? sc2.next() : "");
                output.append(s1).append(" ").append(s2);
                output.append("\n");
            }
        }
        try (PrintWriter pw = new PrintWriter(new File("C:\\Users\\mathe\\Desktop\\Fielddata\\RESULT.txt"))) {
            pw.write(output.toString());
        }
    }
}

【讨论】:

    【解决方案2】:

    可以通过以下方式合并文件夹中的所有txt文件:

    public static void main(String[] args) throws IOException {
            ArrayList<String> list = new ArrayList<String>();
    
            //Reading data files
            try {
    
                File folder = new File("path/inputFolder");
                File[] listOfFiles = folder.listFiles();
    
                for (int i = 0; i < listOfFiles.length; i++) {
                    File file = listOfFiles[i];
                    if (file.isFile() && file.getName().endsWith(".txt")) {
                        BufferedReader t = new BufferedReader (new FileReader (file));
                        String s = null;
                        while ((s = t.readLine()) != null) {                         
                            list.add(s);        
                        }
                        t.close();
                    } 
                }
            }
            catch (IOException e) {
                e.printStackTrace();
            }
    
            //Writing merged data file
            BufferedWriter writer=null;
            writer = new BufferedWriter(new FileWriter("data.output/merged-output.txt"));
            String listWord;              
            for (int i = 0; i< list.size(); i++)
            {
                listWord = list.get(i);
                writer.write(listWord);
                writer.write("\n");
            }
            System.out.println("complited");
            writer.flush();
            writer.close();    
        }
    

    【讨论】:

      【解决方案3】:

      代码如下:

      public class MergeText {
          public static void main(String[] args) throws IOException{
              String output="";
              try(Scanner sc1=new Scanner((new File("A.txt")));
              Scanner sc2=new Scanner((new File("B.txt")))){
      
              while(sc1.hasNext() || sc2.hasNext()){
                  output+=sc1.next() +" "+ sc2.next();
                  output+="\n";
              }
      
              }
      
              try(PrintWriter pw=new PrintWriter(new File("C.txt"))){
              pw.write(output);
              }        
          }
      }
      

      【讨论】:

      • 我不是反对者,但你的 while 应该有 && 而不是 ||。更不用说 StringBuilder/StringBuffer 而不是字符串上的 += 了。所以,是的,他有理由。足够的理由!
      • 我得到了这个异常--->java.util.NoSuchElementException,at line java.util.NoSuchElementException
      • @AvisekPanda 提供一些代码和堆栈跟踪。您应该更新您的问题。
      【解决方案4】:

      您可能想看看BufferedReaderBufferedWriter。 向我们展示您的尝试以及您遇到的困难,我们很乐意提供更多帮助。

      【讨论】:

        猜你喜欢
        • 2016-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-11
        • 2013-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多