A Java Class Object needs to serialized if we need to store or transfer with the state values.
For any IO operations or network operations usually the java class objects needs to sent/received via network via bytes.
During serialization, Object along with state is serialized to bytes and can be processed between the systems as streams.
Below is the simple example for storing the Object with its state into a file., and reading back from the file with its state without losing it.
The Object to be stored, must be serialized.
Class Libraries in Java check each object whether it implements Serializable interface, and if so then the object will be Serializable.
Java class libraries will check all objects implemented the interface , and make them fit for serialization.
Reading an Object which is stored in File or any Stream
ObjectOutputStream
Writing a Object into a File or Stream
Since Serializable is an empty interface, it is a Marker Interface.
Simple Object “Car”, we are going to store/read from file, with the state(values).
Note we have implemented Serializable in Car Class…….
If we are not implementing Serializable Interface, then the values are all stored as null for the fields like carName, numberOfWheels, fuelType etc.,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Car implements Serializable { String carName; int numberOfWheels; String fuelType; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public int getNumberOfWheels() { return numberOfWheels; } public void setNumberOfWheels(int numberOfWheels) { this.numberOfWheels = numberOfWheels; } public String getFuelType() { return fuelType; } public void setFuelType(String fuelType) { this.fuelType = fuelType; } @Override public String toString() { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("[Car:"+this.carName); stringBuffer.append(",NumberOfWheels:" +this.numberOfWheels); stringBuffer.append(",FuelType:"+this.fuelType+"]"); return stringBuffer.toString(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package my.tutorialflow; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class SerializationExample { public static void main(String[] args) { Car benzCar = new Car(); benzCar.setCarName("BENZ"); benzCar.setFuelType("Diesel"); benzCar.setNumberOfWheels(4); Car maruthiCar = new Car(); maruthiCar.setCarName("MARUTHI"); maruthiCar.setFuelType("PETROL"); maruthiCar.setNumberOfWheels(4); List<Car> carList = new ArrayList<Car>(); carList.add(benzCar); carList.add(maruthiCar); ObjectOperator objectOperator = new ObjectOperator(); objectOperator.writeObjectIntoFile("testfile.txt", carList); List<Car> returnedCarList = objectOperator.readObjectIntoFile("testfile.txt"); System.out.println("Car List:" + returnedCarList); } } class ObjectOperator { public void writeObjectIntoFile(String absoluteFilePath, List<Car> car) { FileOutputStream fos = null; ObjectOutputStream objectos = null; try { fos = new FileOutputStream(absoluteFilePath); objectos = new ObjectOutputStream(fos); objectos.writeObject(car); } catch(Exception e) {} finally { try { objectos.close(); fos.close(); }catch(Exception ignore) {} } } public List<Car> readObjectIntoFile(String absoluteFilePath) { FileInputStream fis = null; ObjectInputStream objectis = null; List<Car> carList = null; try { fis = new FileInputStream(absoluteFilePath); objectis = new ObjectInputStream(fis); Object object = objectis.readObject(); carList = (List)object; }catch(Exception e) {} finally { try { objectis.close(); fis.close(); }catch(Exception ignore) {} } return carList; } } |
Result:
1 |
Car List:[[Car:BENZ,NumberOfWheels:4,FuelType:Diesel], [Car:MARUTHI,NumberOfWheels:4,FuelType:PETROL]] |