使用 BufferedReader 从文件中读取
Read from file with BufferedReader
基本上我有一个从 .txt 文件中读取多行的作业。
文本文件中每行有 4 个值,每个值由 2 spaces 分隔。
文件中大约有10行数据。
从文件中获取输入后,程序将其放入数据库中。数据库连接功能工作正常。
我现在的问题是使用 BufferedReader 从文件中读取。
问题是,如果我取消注释底部 3 行中的任何 1 行,BufferedReader 会每隔一行读取一次。如果我不使用它们,那么就会出现异常,因为下一个输入是字符串类型。
我考虑过使用带有 .hasNextLine() 方法的扫描仪。
关于可能是什么问题以及如何解决它有什么想法吗?
谢谢
File file = new File(FILE_INPUT_NAME);
FileReader fr = new FileReader(file);
BufferedReader readFile = new BufferedReader(fr);
String line = null;
while ((line = readFile.readLine()) != null) {
String[] split = line.split(" ", 4);
String id = split[0];
nameFromFile = split[1];
String year = split[2];
String mark = split[3];
idFromFile = Integer.parseInt(id);
yearOfStudyFromFile = Integer.parseInt(year);
markFromFile = Integer.parseInt(mark);
//line = readFile.readLine();
//readFile.readLine();
//System.out.println(readFile.readLine());
}
编辑:.txt 文件的格式有误。缺失值。
但现在我得到一个 ArrayOutOfBoundsException。
编辑编辑:.txt 文件中的另一个错误!原来只有一个 space 而不是一个双。它现在似乎正在工作。但是对于将来如何处理此类文件错误有什么建议吗?
发布的代码的可编译版本可以是
public void read() throws IOException {
File file = new File(FILE_INPUT_NAME);
FileReader fr = new FileReader(file);
BufferedReader readFile = new BufferedReader(fr);
String line;
while ((line = readFile.readLine()) != null) {
String[] split = line.split(" ", 4);
if (split.length != 4) { // Not enough tokens (e.g., empty line) read
continue;
}
String id = split[0];
String nameFromFile = split[1];
String year = split[2];
String mark = split[3];
int idFromFile = Integer.parseInt(id);
int yearOfStudyFromFile = Integer.parseInt(year);
int markFromFile = Integer.parseInt(mark);
//line = readFile.readLine();
//readFile.readLine();
//System.out.println(readFile.readLine());
}
}
上面使用了单个space(" "
而不是原来的" "
)。要拆分任意数量的更改,可以使用正则表达式,例如"\s+"
。当然,也可以使用恰好 2 spaces,如果能反映输入数据的结构的话。
该方法应该如何处理提取的值(例如,将它们返回到某种类型的对象中,或者将它们直接保存到数据库中),取决于使用它的应用程序。
The issue is that if I uncomment any 1 of the 3 lines at the bottom the BufferedReader reads every other line.
正确。如果您放入任何这些代码行,读取的文本行将被丢弃而不进行处理。您已经在 while
条件下阅读。你不需要再读一遍。如果你把这些行中的任何一行放进去,它们将被丢弃而不是继续
基本上我有一个从 .txt 文件中读取多行的作业。 文本文件中每行有 4 个值,每个值由 2 spaces 分隔。 文件中大约有10行数据。
从文件中获取输入后,程序将其放入数据库中。数据库连接功能工作正常。
我现在的问题是使用 BufferedReader 从文件中读取。 问题是,如果我取消注释底部 3 行中的任何 1 行,BufferedReader 会每隔一行读取一次。如果我不使用它们,那么就会出现异常,因为下一个输入是字符串类型。 我考虑过使用带有 .hasNextLine() 方法的扫描仪。
关于可能是什么问题以及如何解决它有什么想法吗? 谢谢
File file = new File(FILE_INPUT_NAME);
FileReader fr = new FileReader(file);
BufferedReader readFile = new BufferedReader(fr);
String line = null;
while ((line = readFile.readLine()) != null) {
String[] split = line.split(" ", 4);
String id = split[0];
nameFromFile = split[1];
String year = split[2];
String mark = split[3];
idFromFile = Integer.parseInt(id);
yearOfStudyFromFile = Integer.parseInt(year);
markFromFile = Integer.parseInt(mark);
//line = readFile.readLine();
//readFile.readLine();
//System.out.println(readFile.readLine());
}
编辑:.txt 文件的格式有误。缺失值。 但现在我得到一个 ArrayOutOfBoundsException。
编辑编辑:.txt 文件中的另一个错误!原来只有一个 space 而不是一个双。它现在似乎正在工作。但是对于将来如何处理此类文件错误有什么建议吗?
发布的代码的可编译版本可以是
public void read() throws IOException {
File file = new File(FILE_INPUT_NAME);
FileReader fr = new FileReader(file);
BufferedReader readFile = new BufferedReader(fr);
String line;
while ((line = readFile.readLine()) != null) {
String[] split = line.split(" ", 4);
if (split.length != 4) { // Not enough tokens (e.g., empty line) read
continue;
}
String id = split[0];
String nameFromFile = split[1];
String year = split[2];
String mark = split[3];
int idFromFile = Integer.parseInt(id);
int yearOfStudyFromFile = Integer.parseInt(year);
int markFromFile = Integer.parseInt(mark);
//line = readFile.readLine();
//readFile.readLine();
//System.out.println(readFile.readLine());
}
}
上面使用了单个space(" "
而不是原来的" "
)。要拆分任意数量的更改,可以使用正则表达式,例如"\s+"
。当然,也可以使用恰好 2 spaces,如果能反映输入数据的结构的话。
该方法应该如何处理提取的值(例如,将它们返回到某种类型的对象中,或者将它们直接保存到数据库中),取决于使用它的应用程序。
The issue is that if I uncomment any 1 of the 3 lines at the bottom the BufferedReader reads every other line.
正确。如果您放入任何这些代码行,读取的文本行将被丢弃而不进行处理。您已经在 while
条件下阅读。你不需要再读一遍。如果你把这些行中的任何一行放进去,它们将被丢弃而不是继续