读取 .txt 信息并将其加载到数组中
Reading .txt information and loading it into an array
所以,基本上,我会有一个名为 "data.txt"
的文件
此文件将包含:
Name;Age;State
示例:
Anne;19;S (Single/Married/etc)
Gustav;18;S
Dinisio;19;C
基本上,我想读取文件,将信息放入一个数组中,然后show/list它,例如:
Name: Anne
Age: 19
State: S
Name: Gustav
Age: 18
State: S
Name: Dinisio
Age: 19
State: C
为此我制作了一个 class 结构如下:
class Person {
String name;
int age;
char state;
}
变量:
int i, option;
String s;
File f = new File("C:\Users\Gustavo\Desktop\data.txt");
Person arrayPerson[] = new Person[4]; // It has 4 persons only
我有以下菜单:
System.out.print("Choose an option: ");
System.out.println("\n1- Load");
System.out.println("2- List");
System.out.print("Option: ");
option= ler.nextInt();
System.out.println("");
switch(option)
{
case 1:
{
Exercicio07Java.openRead(f);
while((s = Exercicio07Java.readLine()) != null)
{
String[] str = s.split(";");
arrayPerson[i] = new Person();
arrayPerson[i].name = str[0];
arrayPerson[i].age = Integer.valueOf(str[1]);
arrayPerson[i].state = str[2].charAt(0);
i++;
}
Exercicio07Java.closeRead();
break;
}
case 2:
{
for(i = 0; i < 4; i++)
{
System.out.println(arrayPerson[i].nome);
System.out.println(arrayPerson[i].idade);
System.out.println(arrayPerson[i].estado);
}
break;
}
}
我或多或少知道如何读取信息,但我不知道如何将它传递到数组中,同时它被 ;
有什么想法吗?
假设你有四行,你可以这样做:
int index = 0;
while//read line in a file {
String[] str = lineFromFile.split(";");//will split line on semicolon and you have name in index 0 of array, age in index 1 of str array and so on
//validate you have all three parameters that you need to create person object.
Person person = new Person(str[0], convertToInt(str[1]), str[2].charAt(0));//assuming you have constructor that accepts name, converted method to convert age to integer, state
arrayPerson[index++]=person;
}
使用String.Split(字符串定界符)它returns一个字符串[]。如果您将整行作为字符串读取并创建了一个新人。见下文。这可能是寻找你的好地方。
for(int i = 0; i < persons.size(); i++) {
String [] info = /**Line From File Here As A String*/.split(";");
persons[i].name = info[0];
persons[i].age = info[1];
persons[i].state = info[2];
}
您可以使用 bufferedreader 读取行并用 ; 分割标记
在此之后
像这样设置数组属性
arrayPerso[i].name = temp[0] //这里的temp是字符串数组,为分割值storage
生成
所以,基本上,我会有一个名为 "data.txt"
的文件此文件将包含:
Name;Age;State
示例:
Anne;19;S (Single/Married/etc)
Gustav;18;S
Dinisio;19;C
基本上,我想读取文件,将信息放入一个数组中,然后show/list它,例如:
Name: Anne
Age: 19
State: S
Name: Gustav
Age: 18
State: S
Name: Dinisio
Age: 19
State: C
为此我制作了一个 class 结构如下:
class Person {
String name;
int age;
char state;
}
变量:
int i, option;
String s;
File f = new File("C:\Users\Gustavo\Desktop\data.txt");
Person arrayPerson[] = new Person[4]; // It has 4 persons only
我有以下菜单:
System.out.print("Choose an option: ");
System.out.println("\n1- Load");
System.out.println("2- List");
System.out.print("Option: ");
option= ler.nextInt();
System.out.println("");
switch(option)
{
case 1:
{
Exercicio07Java.openRead(f);
while((s = Exercicio07Java.readLine()) != null)
{
String[] str = s.split(";");
arrayPerson[i] = new Person();
arrayPerson[i].name = str[0];
arrayPerson[i].age = Integer.valueOf(str[1]);
arrayPerson[i].state = str[2].charAt(0);
i++;
}
Exercicio07Java.closeRead();
break;
}
case 2:
{
for(i = 0; i < 4; i++)
{
System.out.println(arrayPerson[i].nome);
System.out.println(arrayPerson[i].idade);
System.out.println(arrayPerson[i].estado);
}
break;
}
}
我或多或少知道如何读取信息,但我不知道如何将它传递到数组中,同时它被 ;
有什么想法吗?
假设你有四行,你可以这样做:
int index = 0;
while//read line in a file {
String[] str = lineFromFile.split(";");//will split line on semicolon and you have name in index 0 of array, age in index 1 of str array and so on
//validate you have all three parameters that you need to create person object.
Person person = new Person(str[0], convertToInt(str[1]), str[2].charAt(0));//assuming you have constructor that accepts name, converted method to convert age to integer, state
arrayPerson[index++]=person;
}
使用String.Split(字符串定界符)它returns一个字符串[]。如果您将整行作为字符串读取并创建了一个新人。见下文。这可能是寻找你的好地方。
for(int i = 0; i < persons.size(); i++) {
String [] info = /**Line From File Here As A String*/.split(";");
persons[i].name = info[0];
persons[i].age = info[1];
persons[i].state = info[2];
}
您可以使用 bufferedreader 读取行并用 ; 分割标记 在此之后 像这样设置数组属性 arrayPerso[i].name = temp[0] //这里的temp是字符串数组,为分割值storage
生成