如何从 .txt 文件读取到对象数组
How to read from a .txt file into an array of objects
我在 .txt 文件中有以下示例数据
111, Sybil, 21
112, Edith, 22
113, Mathew, 30
114, Mary, 25
所需的输出是
[{"number":"111","name":"Sybil","age":"21" },
{"number":"112","name":"Edith","age":"22"},
{"number":"113","name":"Mathew","age":"30"},
"number":"114","name":"Mary","age":"25"]
遗憾的是,我并没有走得太远,因为我似乎无法从每一行中获取值。相反,这是显示的内容
[一二三]
private void loadFile() throws FileNotFoundException, IOException {
File txt = new File("Users.txt");
try (Scanner scan = new Scanner(txt)) {
ArrayList data = new ArrayList<>() ;
while (scan.hasNextLine()) {
data.add(scan.nextLine());
System.out.print(scan.nextLine());
}
System.out.print(data);
}
如有任何帮助,我将不胜感激。谢谢
目前,您正在跳过几行。 Scanner::nextLine
文档中的引述:
This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
因此,您要向列表中添加一行,并将下一行写入控制台。
获取每一行的数据,可以使用String::split
方法,该方法支持RegEx.
示例:
"line of my file".split(" ")
不太确定要求。如果您只需要知道如何获取值,请将 String.split()
与 Scanner.nextLine()
.
结合使用
以下代码:
private void loadFile() throws FileNotFoundException, IOException {
File txt = new File("Users.txt");
try (Scanner scan = new Scanner(txt)) {
ArrayList data = new ArrayList<>();
while (scan.hasNextLine()) {
// split the data by ", " and split at most (3-1) times
String[] input = scan.nextLine().split(", ", 3);
data.add(input[0]);
data.add(input[1]);
data.add(input[2]);
System.out.print(scan.nextLine());
}
System.out.print(data);
}
}
输出结果如下,您可以自己进一步修改:
[111, Sybil, 21, 112, Edith, 22, 113, Mathew, 30, 114, Mary, 25]
但是,如果您也需要所需的格式,我能得到的最接近的方法是使用 HaspMap
并将其放入 ArrayList
.
以下代码:
private void loadFile() throws FileNotFoundException, IOException {
File txt = new File("Users.txt");
try (Scanner scan = new Scanner(txt)) {
ArrayList data = new ArrayList<>();
while (scan.hasNextLine()) {
// Create a hashmap to store data in correct format,
HashMap<String, String> info = new HashMap();
String[] input = scan.nextLine().split(", ", 3);
info.put("number", input[0]);
info.put("name", input[1]);
info.put("age", input[2]);
// Put it inside the ArrayList
data.add(info);
}
System.out.print(data);
}
}
输出将是:
[{number=111, name=Sybil, age=21}, {number=112, name=Edith, age=22}, {number=113, name=Mathew, age=30}, {number=114, name=Mary, age=25}]
希望这个回答对您有所帮助。
我们可以使用流来编写一些紧凑的代码。
首先我们定义一个record
来保存我们的数据。
Files.lines
将文件读入内存,生成字符串流,每行一个。
我们调用Stream#map
来产生另一个流,一系列的字符串数组。每个数组有三个元素,每行中的三个字段。
我们再次调用 map
,这次是为了生成一个 Person
对象流。我们通过解析行的三个字段中的每一个并将其传递给构造函数来构造每个人对象。
我们调用 Stream#toList
将这些人物对象收集到一个列表中。
我们调用 List#toString
来生成表示人物对象列表内容的文本。
record Person ( int id , String name , int age ) {}
String output =
Files
.lines( Paths.of("/path/to/Users.txt" ) )
.map( line -> line.split( ", " ) )
.map( parts -> new Person(
Integer.parseInt( parts[ 0 ] ) ,
parts[ 1 ] ,
Integer.parseInt( parts[ 2 ] )
) )
.toList()
.toString()
;
如果默认 Person#toString
方法的格式不适合您,请添加该方法的覆盖以生成您想要的输出。
我在 .txt 文件中有以下示例数据
111, Sybil, 21
112, Edith, 22
113, Mathew, 30
114, Mary, 25
所需的输出是
[{"number":"111","name":"Sybil","age":"21" },
{"number":"112","name":"Edith","age":"22"},
{"number":"113","name":"Mathew","age":"30"},
"number":"114","name":"Mary","age":"25"]
遗憾的是,我并没有走得太远,因为我似乎无法从每一行中获取值。相反,这是显示的内容
[一二三]
private void loadFile() throws FileNotFoundException, IOException {
File txt = new File("Users.txt");
try (Scanner scan = new Scanner(txt)) {
ArrayList data = new ArrayList<>() ;
while (scan.hasNextLine()) {
data.add(scan.nextLine());
System.out.print(scan.nextLine());
}
System.out.print(data);
}
如有任何帮助,我将不胜感激。谢谢
目前,您正在跳过几行。 Scanner::nextLine
文档中的引述:
This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
因此,您要向列表中添加一行,并将下一行写入控制台。
获取每一行的数据,可以使用String::split
方法,该方法支持RegEx.
示例:
"line of my file".split(" ")
不太确定要求。如果您只需要知道如何获取值,请将 String.split()
与 Scanner.nextLine()
.
以下代码:
private void loadFile() throws FileNotFoundException, IOException {
File txt = new File("Users.txt");
try (Scanner scan = new Scanner(txt)) {
ArrayList data = new ArrayList<>();
while (scan.hasNextLine()) {
// split the data by ", " and split at most (3-1) times
String[] input = scan.nextLine().split(", ", 3);
data.add(input[0]);
data.add(input[1]);
data.add(input[2]);
System.out.print(scan.nextLine());
}
System.out.print(data);
}
}
输出结果如下,您可以自己进一步修改:
[111, Sybil, 21, 112, Edith, 22, 113, Mathew, 30, 114, Mary, 25]
但是,如果您也需要所需的格式,我能得到的最接近的方法是使用 HaspMap
并将其放入 ArrayList
.
以下代码:
private void loadFile() throws FileNotFoundException, IOException {
File txt = new File("Users.txt");
try (Scanner scan = new Scanner(txt)) {
ArrayList data = new ArrayList<>();
while (scan.hasNextLine()) {
// Create a hashmap to store data in correct format,
HashMap<String, String> info = new HashMap();
String[] input = scan.nextLine().split(", ", 3);
info.put("number", input[0]);
info.put("name", input[1]);
info.put("age", input[2]);
// Put it inside the ArrayList
data.add(info);
}
System.out.print(data);
}
}
输出将是:
[{number=111, name=Sybil, age=21}, {number=112, name=Edith, age=22}, {number=113, name=Mathew, age=30}, {number=114, name=Mary, age=25}]
希望这个回答对您有所帮助。
我们可以使用流来编写一些紧凑的代码。
首先我们定义一个record
来保存我们的数据。
Files.lines
将文件读入内存,生成字符串流,每行一个。
我们调用Stream#map
来产生另一个流,一系列的字符串数组。每个数组有三个元素,每行中的三个字段。
我们再次调用 map
,这次是为了生成一个 Person
对象流。我们通过解析行的三个字段中的每一个并将其传递给构造函数来构造每个人对象。
我们调用 Stream#toList
将这些人物对象收集到一个列表中。
我们调用 List#toString
来生成表示人物对象列表内容的文本。
record Person ( int id , String name , int age ) {}
String output =
Files
.lines( Paths.of("/path/to/Users.txt" ) )
.map( line -> line.split( ", " ) )
.map( parts -> new Person(
Integer.parseInt( parts[ 0 ] ) ,
parts[ 1 ] ,
Integer.parseInt( parts[ 2 ] )
) )
.toList()
.toString()
;
如果默认 Person#toString
方法的格式不适合您,请添加该方法的覆盖以生成您想要的输出。