java 如何将两个文件中的数据逐行写到一个公共文件中

How to write data line by line from the two files into a common file in 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();
        }
    }
}

例如:

文件 1:

Yellow wall 
Red Wall 
Green wall
Black wall

文件 2:

 Red Wall
 Black wall
 Brown wall

结果文件(我的愿望):

  Red Wall
  Black wall

但是这段代码写的文件是这样的:

当前生成的文件:

 Red
 wall
 Black
 wall

您正在使用默认的 Scanner 定界符,即空格。 您应该将分隔符设置为换行符('\r' and/or '\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" ));

但是如果你在一行的末尾有多个换行符,你最终会在输出文件中得到空行。

您必须将两个文件的内容存储在两个 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 包含您需要的所有数据。

您已接近解决方案,但犯了一个简单的错误。也就是说,您正在阅读 firstFile.next(),它会逐字而不是逐行地为您提供,因为您对逐行感兴趣,所以请使用 nextLine() Like:

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() 来避免在您的文本之后出现所有额外的 space,因为 Red Wall_ 不等于 Red Wall 因为单个 space.