2013-09-27 39 views
-3

第一個文本文件 A.txt;合併兩個文本文件逐行使用java

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列:不是有效月份|

針對上述情況,我想從兩個不同的文本文件的兩行合併成一個line.My代碼如下

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(); 
    } 
+0

什麼你想幹什麼? –

+1

到目前爲止你有嘗試過什麼嗎?如果是,請告訴我們! – clamp

+1

你有什麼試圖實現它? – Kayz

回答

1

這裏是代碼:

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); 
     }   
    } 
} 
+0

爲什麼投票?你能提到原因嗎? – Masudul

+0

我不是downvoter,但你的while應該有&&而不是||。更別說StringBuilder/StringBuffer,而不是+ =在字符串上。所以是的,他有理由。足夠的理由! –

+0

謝謝。爲你的好建議。 – Masudul

0

合併從文件夾中的所有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();  
    }