Does the file has to be like that? I mean is it you that decides how the data will be saved?
If you cannot change the way the data are saved then try this:
From what I see you know how to read line by line a file:
while (( line = bf.readLine()) != null)
{
System.out.println("Line read: "+line);
}
Create a class Photo like this:
class Photo {
private String subject = null;
private String location = null;
private String date = null;
private String path = null;
public Photo() {
}
// add get/set methods
}
If the file will always have correct data then:
Read until you find the number of photos:
int numOfPh = 0;
Photo [] photos = null;
while () {
if (line.startsWith("Number of Photos:")) {
numOfPh = ... ; //get the number.
photos = new Photo[numOfPh];
}
}
Then read until you find the line with the "Subject:" . Assuming that the file will always has those 4 attributes with that order then, after you find the line with the "Subject" read the next 4 lines:
int index = 0;
while () {
if (line.startsWith("Subject")) {
String subj = line; // get the subject from the line;
String loc = bf.readLine(); // get the location from the line
String date = bf.readLine(); // get the date from the line
String path = bf.readLine(); // get the path from the line
photos[index] = new Photo();
// set those values to the photos[index]
photos[index].setSubject(subj);
.....
index++;
}
}
And at the end of the method return the photos array.
Now as far as getting the values. The line has this value:
line = "Subject: emily";
line = "Location: boise";
So you cannot do this:
String subj = line; // get the subject from the line;
String loc = bf.readLine(); // get the location from the line
But you need to write a method that takes as argument a String like this: "Name: value" and extract the value. You will call that method to get the Number of Photos as well:
public Stirng getValue(String s) { // s = "Subject: emily"
// method will return emily
}
You can look the java.lang.String API. There is a method indexOf that returns the position of the first occurrence of a specific String: int indexOf = s.indexOf(":");
Now that you know where the ':' is , you can call the substring method of the String class and get what is after the index:
Subject: emily -> substring after the : will return "emily".Don't forget to take into account the "empty space" after the ':'
Else if you are the one who decides how the data are saved, then there is a better way.