java 使用 readline 嵌套 while 循环
我很困惑。我试图循环遍历两个文件,查看第一个文件每一行中的第一个标记,并将其与第二个文件每一行中的第三个标记进行比较。以下是嵌套 while 循环形式的逻辑结构:
BufferedReader reader1 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile1)));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile2),"EUC-JP"));
String line1, line2 = null;
String temp1, temp2 = null;
while ((line1=reader1.readLine()) != null)
{
StringTokenizer st1 = new StringTokenizer(line1);
temp1 = "U"+st1.nextToken();
while((line2=reader2.readLine()) != null)
{
StringTokenizer st2 = new StringTokenizer(line2);
temp2 = st2.nextToken();
temp2 = st2.nextToken();
temp2 = st2.nextToken();
if(temp2.equals(temp1));
{
System.out.println(temp1+" "+temp2);
}
}
}
但是,我在输出中看到的只是第一个文件第一行的第一个标记和第二个文件每一行的第三个标记重复了 6,000(长度文件 2) 次,无论它们是否“相等”。这与它们的不同编码有关吗?我可以看到这对 equals 测试有影响,但为什么循环行为不正确?
干杯, 布兰登
I'm confused. I'm trying to loop though 2 files looking at the first token in every line of the first file and comparing it to the third token of every line of the second file. Here is the logical structure in the form of a nested while loop:
BufferedReader reader1 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile1)));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile2),"EUC-JP"));
String line1, line2 = null;
String temp1, temp2 = null;
while ((line1=reader1.readLine()) != null)
{
StringTokenizer st1 = new StringTokenizer(line1);
temp1 = "U"+st1.nextToken();
while((line2=reader2.readLine()) != null)
{
StringTokenizer st2 = new StringTokenizer(line2);
temp2 = st2.nextToken();
temp2 = st2.nextToken();
temp2 = st2.nextToken();
if(temp2.equals(temp1));
{
System.out.println(temp1+" "+temp2);
}
}
}
However, all I see in the output is the first token from the first line of the first file and the third token from every line of the second file repeated 6,000 (the length of file 2) times regardless of whether they were "equal" or not. Does this have to do with their different encodings? I can see that having an effect on the equals test, but why isn't the loop behaving properly?
Cheers,
Brandon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是;但
它可能不会按预期工作,因为您必须在外循环中重新打开文件 2,否则它只能对文件 1 的第一行正常工作
it's the ; behind the if
But it wouldn't probably work anyway as expected, since you must reopen file 2 within the outer loop, otherwise it will only work correctly for the first line of file 1