0

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

5
  • 2 things we need: Commented Aug 11, 2017 at 9:03
  • 1: post code that compiles so we can check/reproduce things here... Commented Aug 11, 2017 at 9:03
  • 2: I have a problem with this = (Garage)ois.readObject(); is very broad... error messages must be placed here too Commented Aug 11, 2017 at 9:04
  • @ΦXocę웃Пepeúpaツ Yeah I know. I'm at work and I don't have the code on this computer so I can't give the code and the error message. As I said, I'll give all of that as soon as I can. Or at a pause, I'll write a short example Commented Aug 11, 2017 at 9:07
  • this is a keyword, you can't assign to a keyword. Commented Aug 11, 2017 at 10:25

1 Answer 1

1

your class is getting complicated and wrong developed because you are not splitting the responsibilities of every module in the app, what you are trying to do must be the work of some GarageManager class, that class is the responsible for checking if the file exist or not aswell as giving you a garage object (either new created or restored/deserialized from disk)

and example of how that manager can look like is:

class GarageManager {

    public static Garage GetGarage(String garagePath)
            throws FileNotFoundException, IOException, ClassNotFoundException {
        Garage x = null;
        File garageFile = new File(garagePath);
        if (!garageFile.exists() || garageFile.length() == 0) {
            ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(garageFile)));
            x = (Garage) ois.readObject();
            ois.close();
        } else {
            x = new Garage();
        }
        return x;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok in general app. Here, it's an exercise (sorry in French exercices.openclassrooms.com/assessment/… ) And there is no GarageManager

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.