I want to construct an object which has the following behavior :
If the file 'save_object' is empty or doesn't exist create the default object else get back the object that is in the file
I know I will use the Serialization How to write and read java serialized objects into a file But I want to do it IN the class and I don't know how to do it.
I tried with this code (Sorry I just have a part, if it's needeed, I'll the rest as soon as I can)
public class Garage implements Serializable
{
private List<String> cars;
public Garage()
{
File garageFile = new File("garage.txt");
if(!garageFile.exists() || garageFile.length()==0)
{
cars = new List<String>;
System.out.println("Aucune voiture sauvegardée");
}
else
{
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(garageFile)));
this = (Garage)ois.readObject();
}
}
}
I have a problem with this = (Garage)ois.readObject();
and I don't know how to manage it. I have some ideas but all of them are about do one attribut by one and if it's possible, I prefer to avoid to do that