我正在编写一个程序,读取两个文本文件并找出差异 但由于某种原因,我无法打印结果集。我检查了很多次,仍然找不到原因,我希望你们可以帮助我。这是代码。
问题出现在每个打印套件上。
import java.io.*;
import java.util.*;
public class PartOne {
public static void readFileAtPath(String filePathOne, String filePathTwo) {
// Lets make sure the file path is not empty or null
if (filePathOne == null || filePathOne.isEmpty()) {
System.out.println("Invalid File Path");
return;
}
if (filePathTwo == null || filePathTwo.isEmpty()) {
System.out.println("Invalid File Path");
return;
}
Set<String> newUser = new HashSet<String>();
Set<String> oldUser = new HashSet<String>();
BufferedReader inputStream = null;
BufferedReader inputStream2 = null;
// We need a try catch block so we can handle any potential IO errors
try {
// Try block so we can use ‘finally’ and close BufferedReader
try {
inputStream = new BufferedReader(new FileReader(filePathOne));
inputStream2 = new BufferedReader(new FileReader(filePathTwo));
String lineContent = null;
String lineContent2 = null;
// Loop will iterate over each line within the file.
// It will stop when no new lines are found.
while ((lineContent = inputStream.readLine()) != null) {
// Here we have the content of each line.
// For now, I will print the content of the line.
// System.out.println("Found the line: " + lineContent);
oldUser.add(lineContent);
}
while ((lineContent2 = inputStream.readLine()) != null) {
newUser.add(lineContent2);
}
Set<String> uniqueUsers = new HashSet<String>(newUser);
uniqueUsers.removeAll(oldUser);
}
// Make sure we close the buffered reader.
finally {
if (inputStream != null)
inputStream.close();
if (inputStream2 != null)
inputStream2.close();
}
for (String temp : uniqueUsers) {
System.out.println(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
}// end of method
public static void main(String[] args) {
String filePath2 = "userListNew.txt";
String filePath = "userListOld.txt";
readFileAtPath(filePath, filePath2);
}
}
答案 0 :(得分:5)
你的问题是范围。您可以在try块中定义集合,但之后尝试从该块外部访问它。您必须在要使用该变量的同一范围内定义所有变量。
将uniqueUsers的定义移到try块之前。
*编辑以回应您的评论。
您正在从同一输入流中读取两次。第二个while循环应该是从inputStream2读取。
答案 1 :(得分:0)
试试这个
try {
// Try block so we can use ‘finally’ and close BufferedReader
try {
inputStream = new BufferedReader(new FileReader(filePathOne));
inputStream2 = new BufferedReader(new FileReader(filePathTwo));
String lineContent = null;
String lineContent2 = null;
// Loop will iterate over each line within the file.
// It will stop when no new lines are found.
while ((lineContent = inputStream.readLine()) != null) {
// Here we have the content of each line.
// For now, I will print the content of the line.
// System.out.println("Found the line: " + lineContent);
oldUser.add(lineContent);
}
while ((lineContent2 = inputStream.readLine()) != null) {
newUser.add(lineContent2);
}
Set<String> uniqueUsers = new HashSet<String>(newUser);
uniqueUsers.removeAll(oldUser);
for (String temp : uniqueUsers) {
System.out.println(temp);
}
}
// Make sure we close the buffered reader.
finally {
if (inputStream != null)
inputStream.close();
if (inputStream2 != null)
inputStream2.close();
}
} catch (IOException e) {
e.printStackTrace();
}