简体   繁体   中英

Reading from .data file into Java- how do I store the information in an array of arrays?

I have the following Java class, which I'm using to read a .data file:

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:

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.

I assume that this means that the program has read the first two lines of the .data file, but only stored the first.

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.

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM