无法用Java打印集
我正在写一个程序,读入两个文本文件并找出差异 但由于某些原因,我无法打印结果集。我检查了很多次,仍然找不到原因,我希望你们能帮助我。这是代码 该问题发生在每次打印集合时无法用Java打印集,java,io,Java,Io,我正在写一个程序,读入两个文本文件并找出差异 但由于某些原因,我无法打印结果集。我检查了很多次,仍然找不到原因,我希望你们能帮助我。这是代码 该问题发生在每次打印集合时 import java.io.*; import java.util.*; public class PartOne { public static void readFileAtPath(String filePathOne, String filePathTwo) { /
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);
}
}
import java.io.*;
导入java.util.*;
公共课第一部分{
公共静态void readFileAtPath(字符串filePathOne,字符串filepath2){
//让我们确保文件路径不为空或null
if(filePathOne==null | | filePathOne.isEmpty()){
System.out.println(“无效文件路径”);
返回;
}
if(filepathtow2==null | | filepathtow2.isEmpty()){
System.out.println(“无效文件路径”);
返回;
}
Set newUser=newhashset();
Set oldUser=new HashSet();
BufferedReader inputStream=null;
BufferedReader inputStream2=null;
//我们需要一个try-catch块,以便处理任何潜在的IO错误
试一试{
//尝试block,这样我们就可以使用'finally'并关闭BufferedReader
试一试{
inputStream=new BufferedReader(new FileReader(filePathOne));
inputStream2=新的BufferedReader(新的文件读取器(filePathTwo));
字符串lineContent=null;
字符串lineContent2=null;
//循环将迭代文件中的每一行。
//当找不到新行时,它将停止。
而((lineContent=inputStream.readLine())!=null){
//这里我们有每一行的内容。
//现在,我将打印该行的内容。
//System.out.println(“找到行:“+lineContent”);
添加(线条内容);
}
而((lineContent2=inputStream.readLine())!=null){
newUser.add(lineContent2);
}
Set uniqueUsers=newhashset(newUser);
uniqueUsers.removeAll(旧用户);
}
//确保关闭缓冲读取器。
最后{
如果(inputStream!=null)
inputStream.close();
如果(inputStream2!=null)
inputStream2.close();
}
用于(字符串临时:唯一用户){
系统输出打印项次(温度);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}//方法结束
公共静态void main(字符串[]args){
字符串filePath2=“userListNew.txt”;
字符串filePath=“userListOld.txt”;
readFileAtPath(文件路径,文件路径2);
}
}
您的问题在于范围。在try块内定义集合,然后尝试从该块外访问集合。必须在要使用该变量的同一范围内定义所有变量
将uniqueUsers的定义移动到try块之前
*编辑以回应您的评论
您正在从同一输入流读取两次。第二个while循环应该从inputStream2读取。您的问题是范围。在try块内定义集合,然后尝试从该块外访问集合。必须在要使用该变量的同一范围内定义所有变量 将uniqueUsers的定义移动到try块之前 *编辑以回应您的评论 您正在从同一输入流读取两次。第二个while循环应该从inputStream2读取数据。试试这个
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();
}
试试看{
//尝试block,这样我们就可以使用'finally'并关闭BufferedReader
试一试{
inputStream=new BufferedReader(new FileReader(filePathOne));
inputStream2=新的BufferedReader(新的文件读取器(filePathTwo));
字符串lineContent=null;
字符串lineContent2=null;
//循环将迭代文件中的每一行。
//当找不到新行时,它将停止。
而((lineContent=inputStream.readLine())!=null){
//这里我们有每一行的内容。
//现在,我将打印该行的内容。
//System.out.println(“找到行:“+lineContent”);
添加(线条内容);
}
而((lineContent2=inputStream.readLine())!=null){
newUser.add(lineContent2);
}
Set uniqueUsers=newhashset(newUser);
uniqueUsers.removeAll(旧用户);
用于(字符串临时:唯一用户){
系统输出打印项次(温度);
}
}
//确保关闭缓冲读取器。
最后{
如果(inputStream!=null)
inputStream.close();
如果(inputStream2!=null)
inputStream2.close();
}
}捕获(IOE异常){
e、 printStackTrace();
}
试试这个
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();
}
试试看{
//尝试block,这样我们就可以使用'finally'并关闭BufferedReader
试一试{
inputStream=new BufferedReader(new FileReader(filePathOne));
inputStream2=新的BufferedReader(新的文件读取器(filePathTwo));
字符串lineContent=null;
字符串lineContent2=null;
//循环将迭代文件中的每一行。
//当找不到新行时,它将停止。
而((lineContent=inputStream.readLine())!=null){
//这里我们有每一行的内容。
//现在,我将打印该行的内容。
//System.out.println(“找到行:“+lineContent”);
添加(线条内容);
}
而((lineContent2=inputStream.readLine())!=null){
newUser.add(lineContent2);
}
Set uniqueUsers=newhashset(newUser);
uniqueUsers.removeAll(旧用户);
用于(字符串临时:唯一用户){