从 .data 文件读取到 Java - 如何将信息存储在数组数组中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19004104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading from .data file into Java- how do I store the information in an array of arrays?
提问by Noble-Surfer
I have the following Java class, which I'm using to read a .data file:
我有以下 Java 类,我用它来读取 .data 文件:
import java.io.*;
import java.util.Scanner;
public class ReadFile {
static File file;
String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
String[] data = new String[100];
String filename = "D:\Users\Elgan Frost\Desktop\careers\Smart Stream Associate Software Engineer (Java) - Bristol\assessment\srcPerson.data";
public ReadFile(File f) throws FileNotFoundException {
// TODO Auto-generated constructor stub
try{
//File = new File("D:\Users\Elgan Frost\Desktop\careers\Smart Stream Associate Software Engineer (Java) - Bristol\assessment\srcPerson.data");
f = new File("D:\Users\Elgan Frost\Desktop\careers\Smart Stream Associate Software Engineer (Java) - Bristol\assessment\src\Person.data");
Scanner scanner = new Scanner(f);
while(scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}
scanner.close();
}
finally{
}
}
public void read(File file2) throws IOException{
FileReader fr = new FileReader("D:\Users\Elgan Frost\Desktop\careers\Smart Stream Associate Software Engineer (Java) - Bristol\assessment\src\Person.data");
BufferedReader br = new BufferedReader(fr);
String line;
int i = 0;
while((line = br.readLine())!= null){
data[i] = line;
System.out.println(data[i]);
i++;
}
br.close();
String[] dataNew = new String[i];
System.arraycopy(data, 0, dataNew, 0, i);
data = dataNew;
System.out.println("Data length: " + data.length);
}
}
To give you a more complete understanding of my code, I also have the following Java class, which contains my main method:
为了让您更全面地了解我的代码,我还有以下 Java 类,其中包含我的 main 方法:
import java.io.*;
public class Person {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ReadFile rf = new ReadFile(f);
rf.read(ReadFile.file);
}
static File f;
public String id;
public String lastName;
public String firstName;
public String street;
public String city;
public void readPerson() throws FileNotFoundException{
//File f = new File("Smart Stream Associate Software Engineer (Java) - Bristol/assessment/src/Person.java");
//InputStream IS = new FileInputStream(f);
}
}
Currently, when I run my program, the console displays two lines that say, "Person.data...." and give the filepath of the .data file that is being read, which will have come from the lines System.out.println(scanner.nextLine());
and System.out.println(data[i]);
in my ReadFile.java class, as well as the line "Data length: 1", which will have come from the line System.out.println("Data length: " + data.length);
at the end of the class.
目前,当我运行我的程序时,控制台会显示两行,上面写着“Person.data ....”并给出正在读取的 .data 文件的文件路径,这将来自这些行System.out.println(scanner.nextLine());
和System.out.println(data[i]);
我的 ReadFile .java 类,以及“数据长度:1”行System.out.println("Data length: " + data.length);
,该行来自类末尾的行。
I assume that this means that the program has read the first two lines of the .data file, but only stored the first.
我假设这意味着程序已经读取了 .data 文件的前两行,但只存储了第一行。
What I want to happen, is for it to read the first line, store that in a String, then divide the String up into separate Strings wherever a 'space' appears, and store each of those Strings in an element of the array of Strings that I created at the start of the class- by using the line:
我想要发生的是让它读取第一行,将其存储在一个字符串中,然后在出现“空格”的任何地方将字符串分成单独的字符串,并将这些字符串中的每一个存储在字符串数组的一个元素中我在课程开始时创建的 - 通过使用以下行:
String[] columns = new String[]{"personID", "lastName", "firstName", "street", "city"};
The separate Strings should be stored to each one in that order- so the first String (before the first space in the original String) would be stored in "personID", the second (after the first space, before the second) would be stored in "lastName", etc.
单独的字符串应按该顺序存储到每个字符串中 - 因此第一个字符串(在原始字符串中的第一个空格之前)将存储在“personID”中,第二个(在第一个空格之后,第二个之前)将被存储在“姓氏”等
What I then want to do, is read each of the next lines in the file in turn, storing the elements from each line into the next 'row' of my 'columns' String array (basically creating a table of data, using an array of arrays) whereby the first element of each array is a "personID", the second is a "lastName", etc.
然后我想要做的是依次读取文件中的每一行,将每一行的元素存储到我的“列”字符串数组的下一个“行”中(基本上创建一个数据表,使用数组数组),其中每个数组的第一个元素是“personID”,第二个元素是“lastName”,等等。
I want to do this until I reach the end of the file, and then print the contents of a couple of elements of the 'columns' array, just to show that it has worked.
我想这样做直到我到达文件的末尾,然后打印“列”数组的几个元素的内容,只是为了表明它已经工作。
I'm not sure how I would go about this, and was just wondering if anyone could point me in the right direction?
我不确定我会怎么做,只是想知道是否有人可以指出我正确的方向?
采纳答案by Pratik Shelar
final Scanner in = new Scanner(new FileReader("filename.txt"));
final List<Person> persons= new ArrayList<Person>();
while (in.hasNext()) {
final String[] columns = in.next().split(" ");
final Person person = new Person();
Person.setId(column[0]);
Person.setName(column[1]);
// like this you can set all the fields
persons.add(person);
}
You can then loop through few records from the columns list and display the same
然后,您可以遍历列列表中的几条记录并显示相同的
As suggested in comment according to OOPS the best design would be to have a person object and storing the data from file as list of Person objects
正如根据 OOPS 在评论中所建议的,最好的设计是拥有一个 person 对象并将文件中的数据存储为 Person 对象列表