java阅读行每隔一行跳过一行
我正在制作一个小程序,基本上是从txt文件加载的。txt文件包含以下数据:
NAME1, xx, xx, xx, xx (Where XX are numbers)
NAME2, xx, xx, xx, xx
etc...
此文件没有设置结尾,因为以后可以对其进行编辑以添加其他名称。 我必须阅读的代码如下:
private void doLoadProfile() {
String filePath = System.getProperty("user.dir") + File.separator + "profiles.txt";
System.out.println(filePath);
try {
FileInputStream fis = new FileInputStream(filePath);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));
while (in.readLine() != null) {
displayLog.appendText(in.readLine() + "\n");
}
} catch (FileNotFoundException e) {
displayLog.appendText("\n Error: file not found" + e.toString());
} catch (IOException e) {
displayLog.appendText("\n Error: " + e.toString());
}
}
然而,这只输出每隔一行,出于某种原因,它跳过了几行(我有一个包含4行的txt文件,我只得到第2行和第4行)。我试着再加上两行,结果只得到了第二行、第四行和第六行
# 1 楼答案
你在打电话。readLine()两次(一次在while语句中读取第一行,另一次在appendText中读取第二行)。将while语句中的值缓存为字符串,以后使用该值