我正在制作一个小程序,基本上是从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 行(。我尝试添加额外的 2 行,再次,只得到第 2、4 和 6 行。
你调用 in.readLine(( 两次(一次在 while 语句中,读取第一行,另一次在 appendText 中,读取第二行(。将 while 语句中的值缓存在字符串中,稍后改用该值。