Hi All,
I've a little problem with writing and saving to a file
I'm close but I can't seem to grasp what i'm doing wrong here.
Trying to save student object(s) to allstudents.txt
This is my loadStudent method;
public student[] loadStudent() throws IOException, ClassNotFoundException
//loads the accounts and returns them
{
ObjectInputStream fileInput = new ObjectInputStream(new FileInputStream("allstudents.txt"));
student[] returnStudent = new student[MAX_STUDENTS];
try
{
for (int i = 0; i < MAX_STUDENTS; i ++)
{
returnStudent[i] = (student)fileInput.readObject();
System.out.println(returnStudent[i]);
}
}
catch(EOFException e)
{
fileInput.close();
return returnStudent;
}
return returnStudent;
}
My save student is as follows
public void saveStudents(student[] saveStudents) throws IOException
//saves the accounts to file
{
ObjectOutputStream fileOutput = new ObjectOutputStream(new FileOutputStream("allstudents.txt",true)); //the thing to save
arrayStudent = saveStudents;
for (int i = 0; i < arrayStudent.length; i ++)
{
fileOutput.writeObject(arrayStudent[i]);
System.out.println(arrayStudent[i]);
}
}
What I don't get is the file allstudents.txt is growing each time I save the program
but next time I load nothing loads into memory, so there is error with my logic somewhere
Those methods are within a class "io"
My main class loads them as thus
io memory = new io();
memory.loadStudent();
Any assistance would be gratefully received !