I have a trouble with my java project. After several searches, i'm still blocked.
I have a class named ListFromFile that extends ArrayList :
public class ListFromFile<T> extends ArrayList<T> implements Serializable {
private String listFile;
public ListFromFile() {
this.listFile = null;
}
public ListFromFile(String file) {
this.listFile = file;
}
public void loadDataFromFile() { // Deserialize from file
try {
FileInputStream fileInput = new FileInputStream(this.listFile);
if ( fileInput.read() == -1 ) { // If file is empty
this.saveDataInToFile();
} else {
ObjectInputStream objectInput = new ObjectInputStream(fileInput);
this.addAll((ArrayList) objectInput.readObject());
objectInput.close();
}
fileInput.close();
} catch ( IOException | ClassNotFoundException e ) {
e.printStackTrace();
}
}
public void saveDataInToFile() { // Serialize the ArrayList
try {
FileOutputStream fileOutput = new FileOutputStream(this.listFile, true);
ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(this);
objectOutput.flush();
objectOutput.close();
fileOutput.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
In my main class BZ2MServer, i've created some objects with ListFromFile :
public class BZ2MServer {
private static ListFromFile<Specialite> specialites = new ListFromFile<Specialite>("liste_specialites.txt");
private static ListFromFile<Module> modules = new ListFromFile<Module>("liste_modules.txt");
private static ListFromFile<Professeur> professeurs = new ListFromFile<Professeur>("liste_professeurs.txt");
private static ListFromFile<Etudiant> etudiants = new ListFromFile<Etudiant>("liste_etudiants.txt");
private static ListFromFile<Note> notes = new ListFromFile<Note>("liste_notes.txt");
public static void main(String[] args) {
specialites.loadDataFromFile();
}
}
When the application starts, it loads the lists saved in files. That's why i execute saveDataInToFile() inside that loadDataFromFile() method.
By this way, the files should contain empty ArrayList at the first time we launch the application.
The result is that for the first launch, there is no issue. However, after this, i always have this from the compiler :
java.io.StreamCorruptedException: invalid stream header: ED000573
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at bz2m.list.ListFromFile.loadDataFromFile(ListFromFile.java:31)
at bz2m.server.BZ2MServer.main(BZ2MServer.java:22)
I have tested this in the main of BZ2MServer:
public static void main(String[] args) {
Specialite test = new Specialite(1, "Test");
specialites.add(test2);
specialites.saveDataInToFile();
}
Then, i removed the previous code, used LoadDataFromFile() to get the previous datas. And i got the error. Then, i removed that condition in ListFromFile class :
if ( fileInput.read() == -1 ) {
this.saveDataInToFile();
}
And it worked. However, that condition was necessary in the case the file is empty. Without it, and if the file is empty, i get this :
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at bz2m.list.ListFromFile.loadDataFromFile(ListFromFile.java:31)
at bz2m.server.BZ2MServer.main(BZ2MServer.java:22)
I hope that i have give you all the information you need to help me. If you need, i can upload the entire project (not huge).
Thank you !