Hi i'm having a little problem reading from a text file into arrays. In my program, it is saved with movies and actors, and when saved, a text file is created with movies first and then actors, with each movie and actor having a seperate line.
My problem is occuring when trying to read up until a point so that all the movies go in a movies array and all the actors go into an actor array. I have tried putting a write(newline) under the last movie in the file and then doing a while loop until the line is null to then end that loop and go onto a new loop for actors until the end of the file but this does not work.
Really am stuck trying to get this to work, i'm quite a beginner at java so any and all help would be appreciated.
Thanks in advance.
[EDIT]
My problem is that everything in the file goes into one array, the movie array. Instead, I would like the movies to go into the movie array, and the actors to go into the actor array. Below is my code for saving and loading the file so far:
public void save(String fileName) throws FileNotFoundException {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
for ( int i = 0; i < nbrMovies; i++)
{
writer.write(movies[i].getName());
writer.newLine();
}
writer.newLine();
for (int n = 0; n < nbrActors; n++)
{
writer.write(actors[n].getFullName());
writer.newLine();
}
writer.close();
} catch(IOException ex) {
ex.printStackTrace();
}
}
public void load(String fileName) throws FileNotFoundException {
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
addMovie(line);
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
Thanks again :) [END EDIT]
[EDIT2]
Example file below :)
testmovie
testmovie2
Firstname Secondname Firstname2 Secondname2
[END EDIT2]
while ((line = reader.readLine()) != null)) because an empty line is notnull, it's just empty. Maybe you can use some kind of mark as is used usually in the configuration files, something like[Movies]to mark the start of the Movies section, and[Actors]to mark the start of the Actors section, and then look for them when you are loading the file. Note:readlineonly returnsnullwhen it reaches the end of the stream.