我编写了以下代码,内部for
循环工作正常,但外部循环不会迭代。这是我的代码:
BufferedReader userfeatures = new BufferedReader(new FileReader("userFeatureVectorsTest.csv"));
BufferedReader itemfeatures = new BufferedReader(new FileReader("ItemFeatureVectorsTest.csv"));
while ((Uline = userfeatures.readLine()) != null)
{
for (String Iline = itemfeatures.readLine(); Iline != null; Iline = itemfeatures.readLine())
{
System.out.println(intersect(Uline, Iline).size());
System.out.println(union(Uline, Iline).size());
}
}
userfeatures.close();
itemfeatures.close();
}
它找到first line of the first file
与every line of the 2nd file
的交集和并集,然后停止。但是,我需要继续并对第一个文件的下一行重复相同的过程。所以,我认为有一些与我的外部while循环有关的问题,但是我找不到有什么问题:|
有人可以帮帮我吗?
感谢
答案 0 :(得分:1)
itemfeatures。否则,您在第一次迭代后完全读取您的itemfeatures
答案 1 :(得分:1)
正如人们所说, for 循环完全读取第二个文件,因此您每次都必须再次阅读。
因此,在 for 循环之前重新创建Reader,并在 for 循环之后关闭它。
简单地说,每次从文件1中读取一行时,您必须打开,完全读取并关闭文件2:
BufferedReader userfeatures = new BufferedReader(new FileReader("userFeatureVectorsTest.csv"));
while ((Uline = userfeatures.readLine()) != null)
{
BufferedReader itemfeatures = new BufferedReader(new FileReader("ItemFeatureVectorsTest.csv"));
for (String Iline = itemfeatures.readLine(); Iline != null; Iline = itemfeatures.readLine())
{
System.out.println(intersect(Uline, Iline).size());
System.out.println(union(Uline, Iline).size());
}
itemfeatures.close();
}
userfeatures.close();
}