[英]Java - Read loosely structured data from file
我目前正在尝试了解 Java 代码的行为方式以及如何处理输入和输出文件。 尽管我了解如何逐行读取文件的内容并将它们放入数组中,但我很难理解如何从文件中读取每 n 行出现的某些值,然后将它们放入数组中. 例如,我有一个如下所示的输入测试文件:
2
Australia
John
42
Blue
USA
Jeremmy
15
Black
第一行是数组的大小。 以下几行是我想从文件中读取并放入所述数组的内容(在本例中,我将其设为:国家、姓名、年龄、眼睛颜色)。 换句话说,我想读取一些每四行出现一次的对象属性,以便稍后我可以在选择时将它们打印出来,例如其中一个人。 现在我被困在这里,不知道如何前进,因为大多数人没有尝试对这样的文件进行操作。
private static String[] readPeople(File file) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String sizeText = br.readLine();
int size = Integer.parseInt(sizeText);
String[] peopleSet= new String[size];
for (int i = 1; i < size; i++) {
if (i % 2 != 0) {
String peopleInfo= br.readLine();
peopleSet[i] = peopleInfo;
}
}
return peopleSet;
} catch (IOException ex) {
System.err.println(ex);
return new String[0];
}
}
使行冒号分隔可能更容易
您的条目是否以冒号分隔在文件中的值之间:
Australia:John:42:Blue
USA:Jeremmy:15:Black
然后在您的文件解析器中:
private static List<Person> readPeople(File file) {
List<Person> people = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line = "";
while((line = br.readLine()) != null) {
String[] args = line.split(":");
String country = args[0];
String name = args[1];
int age = Integer.parseInt(args[2]);
String eyeColor = args[3];
Person p = new Person(name, country, age, eyeColor);
people.add(p);
}
} catch (IOException ex) {
System.err.println(ex);
}
return people;
}
最后定义 Person 类
class Person {
String name;
String country;
int age;
String eyeColor;
public Person(String name, String country, int age, String eyeColor) {
this.name = name;
this.country = country;
this.age = age;
this.eyeColor = eyeColor;
}
@Override
public String toString() {
return String.format("%s:%s:%d:%s", country, name, age, eyeColor);
}
}
根据需要添加错误检查和 getter/setter
这将返回文件中定义的人员列表,您可以通过在返回列表对象上调用.size()
来检查列表的大小。
更新
添加toString()
覆盖以创建冒号单独条目,以便将其写出到文件或控制台
希望这可以帮助
我给你这个代码。
File Read_File = new File("file_path");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(Read_File));
String line;
int line_number = 0;
int size = Integer.parseInt(br.readLine());
for (int try_ = 0; try_ < size; try_++) {
String read = br.readLine();
for(int try_four = 0; try_four<4; try_four++) {
//save input operation
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
}
}
但使用列表是最好的方法。
问题是您的代码仅在条件为真时从文件中读取。 无论您循环多少次,文件内容仅在您实际使用br.readLine()
阅读时才会前进。
for (int i = 1; i < size; i++) {
if (i % 2 != 0) {
String peopleInfo= br.readLine();
peopleSet[i] = peopleInfo;
}
}
也不清楚你想读哪几行。 这段代码回路size
次(2),但只读过线隔日一次,因此它只会读“澳大利亚”。
如果您想读取每个四人组中的第三行的人名(“John”和“Jeremmy”),您可以这样做:
for (int i = 1; i < size; i++) {
for (int j = 0; j < 4; j++) {
String line = br.readLine();
if (j % 4 != 2) {
people[i] = line;
}
}
}
这读取所有八行(从“澳大利亚”到“黑色”)。 对于每个人, j
的值将从0
到3
; 因此,第三行是当j % 4 == 2
。
它将名称存储到长度为 2 的数组people
中。我从peopleSet
重命名它,因为它不是 Java Set
。 原始名称具有误导性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.