2015-05-20 43 views
0

而在一個合併兩個文件,我面臨的一個共同文件(常見的內容)如何通過從兩個文件行寫入數據線到Java中

public class myFileReader { 

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

     List<String> firstFileList = new ArrayList<String>(); 
     List<String> secondFileList = new ArrayList<String>(); 

     List<String> missingRecordsInFile2 = new ArrayList<String>(); 

     Scanner firstFile = new Scanner(new FileReader(new File("C://write1.txt"))); 
     Scanner secondFile = new Scanner(new FileReader(new File("C://write2.txt"))); 

     FileWriter fWriteOne = new FileWriter(new File("C://read1.txt")); 


     while (firstFile.hasNext()) { 
      firstFileList.add(firstFile.next()); 
     } 

     while (secondFile.hasNext()) { 
      secondFileList.add(secondFile.next()); 
     } 

     try { 
      for (String fileOne : firstFileList) { 
       boolean value = secondFileList.contains(fileOne); 
       if (value) { 
        missingRecordsInFile2.add(fileOne); 
        fWriteOne.write(fileOne); 
        fWriteOne.write(System.getProperty("line.separator")); 

       } 
      } 
     } finally { 
      fWriteOne.close(); 
     } 
    } 
} 

例如:

FILE 1:

Yellow wall 
Red Wall 
Green wall 
Black wall 

FILE 2:

Red Wall 
Black wall 
Brown wall 

產生的文件(我的願望):

Red Wall 
    Black wall 

但是這樣的代碼寫入文件:

電流產生的文件:

Red 
wall 
Black 
wall 
+0

那麼,你想獲得這兩個文件中常見的行嗎? – Kartic

+0

是的,正好。我會很樂意爲你提供幫助。 –

回答

1

您即將接近解決方案,但犯了一個簡單的錯誤。即你是firstFile.next()它給你一個字一個字的,而不是一行行,因爲你有興趣在逐行所以使用nextLine()喜歡看書看:

while (firstFile.hasNext()) { 
    firstFileList.add(firstFile.nextLine().trim()); 
} 

while (secondFile.hasNext()) { 
    secondFileList.add(secondFile.nextLine().trim()); 
} 

try { 
    for (String fileOne : firstFileList) { 
     boolean value = secondFileList.contains(fileOne); 
     if (value) { 
      missingRecordsInFile2.add(fileOne); 
      fWriteOne.write(fileOne); 
      fWriteOne.write(System.getProperty("line.separator")); 
     } 
    } 
} finally { 
    fWriteOne.close(); 
} 

更多

我建議使用String.Trim()以避免文本之後的所有多餘空格,因爲Red Wall_由於單個空格不等於Red Wall

+0

是的!你的回答幫了我很多!非常感謝你! –

+0

快樂是我的@КириллКраснобаев – Sarz

1

您使用的是默認掃描儀的分隔符,這是空白。 你應該設置分隔符爲換行符(或「\ r」和/或「\ n」),這樣

Scanner firstFile = new Scanner(new FileReader(new File("C://write1.txt"))); 
Scanner secondFile = new Scanner(new FileReader(new File("C://write2.txt"))); 
firstFile.useDelimiter(Pattern.compile("[\\r\\n]+")); 
secondFile.useDelimiter(Pattern.compile("[\\r\\n]+")); 

也許你也可以使用簡單的

myScanner.useDelimiter(System.getProperty("line.separator")); 

但如果你有在一行的結尾有多個換行符,最終會在輸出文件中出現空行。

+0

感謝您的幫助。 –

1

你要存儲的兩個文件的內容有兩種List像你一樣:

List<String> firstFileList = new ArrayList<String>(); 
List<String> secondFileList = new ArrayList<String>(); 

/***** Start : This is dummy data ********/ 
firstFileList.add("Yellow wall"); 
firstFileList.add("Red Wall"); 
firstFileList.add("Green wall"); 
firstFileList.add("Black wall"); 

secondFileList.add("Red Wall"); 
secondFileList.add("Black wall"); 
secondFileList.add("Brown wall"); 
/***** End: This is dummy data ********/ 

firstFileList.retainAll(secondFileList); 

現在firstFileList包含了所有你需要的數據。

+0

你的答案是精確的,但在這種情況下循環是強制性的,因爲他希望它在文件中寫入。 – Sarz

+0

當然可以。但我猜想,主要問題是確定兩個文件中的通用行(如註釋部分所述),我試圖只關注該區域。感謝您的輸入。 – Kartic

+0

感謝您的幫助。 –

相關問題