Reputation: 57
I am working on a project where I need to read text from a file and then store particular parts as variables.
Say I had the text file example.txt
John
Smith
32
1991
I would want to assign the first line as:
String firstName; -> John
and so on.
I have this so far.
public void load(String fileName){
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
This code will print the contents of the file, which is a step in the right direction, but how to go about reading per line?
As I don't really want to get into the habit of storing whole files in memory.
I know you can convert the text with
parseInt()
and similar methods but I'm just trying to get my head around extracting values.
Upvotes: 2
Views: 108
Reputation: 105
You can try this too.
File file = new File("your file location");
Scanner scan = new Scanner(file);
while(scan.hasNextLine()){
//Assign to your pojo
System.out.println(scan.next());
}
Upvotes: 0
Reputation: 347334
You could do something like...
while ((sCurrentLine = br.readLine()) != null) {
String firstName = sCurrentLine;
String lastName = br.readLine();
String age = br.readLine();
String dob = br.readLine();
}
But this makes quite an assumption about the file requirements, it assumes that there are 4 lines per entry.
You could place each entry on it's own line and use a deliminator to separate them, for example
John;Smith;32;1991
And then use something like...
while ((sCurrentLine = br.readLine()) != null) {
String parts[] = currentLine.split(";");
String firstName = parts[0];
String lastName = parts[1];
String age = parts[2];
String dob = parts[3];
}
to read each entry. This gives you a little more control over processing the entries, as you can check the length of parts
array to validate that it has the number of expected values.
Once you've got an entry, you'll want a better way to manage each entry, I'd create a simple POJO to hold the values, maybe something like...
public class Person {
private final String fisrtName;
private final String lastName;
private final int age;
private final int dob;
public Person(String fisrtName, String lastName, int age, int dob) {
this.fisrtName = fisrtName;
this.lastName = lastName;
this.age = age;
this.dob = dob;
}
public String getFisrtName() {
return fisrtName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public int getDob() {
return dob;
}
}
Then you might be able to use List
or Map
to store them, which would make it easier to manage...
List<Person> people = new ArrayList<>(25);
//...
while ((sCurrentLine = br.readLine()) != null) {
String parts[] = currentLine.split(";");
String firstName = parts[0];
String lastName = parts[1];
String age = parts[2];
String dob = parts[3];
people.add(new Person(firstName, lastName, Integer.parseInt(age), Integer.parseInt(dob));
}
Another solution might be to use a Scanner
, which would allow you to use next
and nextInt
to parse each entry
So, if we re-structure the to the following format...
John Smith 32 1991
You could simply use
while ((sCurrentLine = br.readLine()) != null) {
Scanner scanner = new Scanner(sCurrentLine);
String firstName = scanner.next();
String lastName = scanner.next();
int age = scanner.nextInt();
int dob = scanner.nextInt();
people.add(new Person(firstName, lastName, age, dob);
}
instead
Upvotes: 4