我正在尝试找出两个文本文件之间的差异,并尝试在第三个文件中写入差异!!!我这样做非常成功,但是我的文件太大了,它有将近 200000 行,当我将它存储到 arrylist 时,它只存储数据直到第 6482 行。我不明白为什么会这样。有人可以帮我吗?这是我的代码
import java.io.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SearchThe {
public static void main(String args[]) {
try {
// Open the file c:\test.txt as a buffered reader
Scanner s = new Scanner(new File("D://abc.txt"));
Scanner s2 = new Scanner(new File("D://xyz.txt"));
// Scanner s = new Scanner(new File("D://file1.txt"));
// Scanner s2 = new Scanner(new File("D://file2.txt"));
ArrayList<String> file1_list = new ArrayList() ;
ArrayList<String> file2_list = new ArrayList();
// file1_list.ensureCapacity(193225);
// file2_list.ensureCapacity(193225);
while (s.hasNext()) {
file1_list.add(s.next());
}
s.close();
while (s2.hasNext()) {
file2_list.add(s2.next());
}
s2.close();
FileWriter stream = new FileWriter("D://file1_copy.csv");
BufferedWriter abc = new BufferedWriter(stream);
for(int i=0;i<file1_list.size();i++)
{
System.out.println("File 1 data" +file1_list.get(i));
abc.write(file1_list.get(i));abc.newLine();
}
abc.close();
//
// Start a line count and declare a string to hold our current
int linecount = 0;
String line;
// Let the user know what we are searching for
// for(int i=0;i<file1_list.size();i++)
// { // Loop through each line, stashing the line into our line
FileWriter fstream = new FileWriter("D://result.txt");
BufferedWriter out = new BufferedWriter(fstream);
for(int j =0;j<file1_list.size();j++)
{
System.out.println("\n\nSearching for " + file1_list.get(j) + " in file 1...");
if(file2_list.contains(file1_list.get(j)))
{
System.out.println(file1_list.get(j) +" found in a file 2");
}
else
{
System.out.println(file1_list.get(j) +" not found in a file 1 ");
out.write(file1_list.get(j));out.newLine();
}
}
out.close();
}
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
}
}