0

Suppose that I have an abstract class "Vehicle" and 2 other child classes inheriting from the Vehicle class called "Car" and "Bike".

So I can create a series of car and bike objects randomly:

Vehicle car = new Car();
Vehicle bike = new Bike();

And add them all to an Arraylist:

Arraylist<Vehicle> vehicles = new Arraylist<>();

Is it possible to write these objects to a single text file and also read it back from the text file to the Arraylist according to the specific object types(car, bike) without maintaining different textfiles for each type

RuFerdZ
  • 46
  • 5
  • 1
    Welcome to Stack Overflow! Please turn to the [help] to learn how/what to ask here. Just dropping requirements "this is what I want" isn't appreciated. When you try something yourself, and you get stuck with a specific problem, we will gladly help. But please understand that this place is not intended to give guidance with the possibly many steps required to get you from your vision to a working program. – GhostCat Dec 02 '20 at 10:03
  • Do you just want to save all vehicles to a text file and load it from it, again? – dan1st Dec 02 '20 at 10:04
  • 1
    Thing is: there are many ways to *serialize* Java objects. You can use the (binary based) legacy built-in java serialisation (as said: binary, wont result in human readable text files). Or you write your OWN textual serialization, where your classes know how to write/read back themselves from text. Or you can use libraries such as gson or jackson to somehow serialize your objects using JSON strings for example. In other words: there are really many different ways to approach this. – GhostCat Dec 02 '20 at 10:04
  • Coming from there, for a Java newbie, you should probably first research the "built in" stuff: https://www.baeldung.com/java-serialization ... and afterwards, you look at using JSON . – GhostCat Dec 02 '20 at 10:06
  • @dan1st yes i want to do that – RuFerdZ Dec 02 '20 at 10:08

2 Answers2

1

Yes,

If the abstract class implements the serializable interface, you can actually serialize the child classes as well.

Therefore by making the vehicle class serializable, you can iterate over the array list and use the the ObjectOutputStream and FileOutputStream to serialize the child objects.

Refer: https://www.geeksforgeeks.org/object-serialization-inheritance-java/

1

If you want to store objects in a file, I'd advise you use object serialization. This link has useful details on how to serialise and deserialise a list

This is how you serialize a list:

ArrayList<Vehicle> vehicles= new ArrayList<>();
         
        vehicles.add(new Vehicle());
        vehicles.add(new Bike());
        vehicles.add(new Car());
 
        try
        {
            FileOutputStream fos = new FileOutputStream("vehiclesData");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(vehicles);
            oos.close();
            fos.close();
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
        }

To deserialize use the following:


        ArrayList<Vehicle> vehicles= new ArrayList<>();
         
        try
        {
            FileInputStream fis = new FileInputStream("vehiclesData");
            ObjectInputStream ois = new ObjectInputStream(fis);
 
            vehicles= (ArrayList) ois.readObject();
 
            ois.close();
            fis.close();
        } 
        catch (IOException ioe) 
        {
            ioe.printStackTrace();
            return;
        } 
        catch (ClassNotFoundException c) 
        {
            System.out.println("Class not found");
            c.printStackTrace();
            return;
        }
         
        //Verify list data
        for (Vehicle vehicle : vehicles) {
            System.out.println(vehicle);
        }

Make sure all your classes implement serializable interface

Piaget Hadzizi
  • 702
  • 8
  • 15
  • As written in the comments above: this doesnt fulfil the requirement given by the OP. The OP wanted a TEXT file. Java serialization doesnt end up as "text". – GhostCat Dec 02 '20 at 12:18