Java Serialization – Example

 IT, Java  Kommentare deaktiviert für Java Serialization – Example
Mai 212013
 

First create an object that you want to store persistently (serialize). I use the object „Car“. Please ensure that your class implements the interface Serializable

package com.philipp.testing;

import java.io.Serializable;

public class Car implements Serializable {

	public enum Brand {
		BMW, Mercedes, Porsche, Audi
	};

	private Brand brand;
	private String model = "";
	private int hoursepower = 0;
	private int price = 0;

	public Car(Brand brand, String model, int hoursepower, int price) {
		super();
		this.brand = brand;
		this.model = model;
		this.hoursepower = hoursepower;
		this.price = price;
	}

	public Brand getBrand() {
		return brand;
	}

	// removed some "Getter" and "Setter" for this example..

	public void setPrice(int price) {
		this.price = price;
	}

}

Now create a „testing“ class that will create a new car and serializes it:

package com.philipp.testing;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.philipp.testing.Car.Brand;

public class testingTheSerialization {

	public static void main(String[] args) {

		// create a new Car
		Car myCar = new Car(Brand.Porsche, "911 Carrera S", 400, 20000);

		// now store the car in a file (serialize it):
		System.out.println("Serialize the car to file myCar.file");
		doSerialize(myCar);
		System.out.println("Serialization done");

		// now try to read the file and parse the object to a new car:
		Car myCarFromFile = null;
		myCarFromFile  =  (Car) doDeSerialize("myCar.file");
		System.out.println("Your car is: " + myCarFromFile.getBrand() +  " - " + 
								myCarFromFile.getModel() + " - " +
								myCarFromFile.getHoursepower() + "hourspower - " + 
								myCarFromFile.getPrice() + "Euro");

		System.out.println("Deserialization done...");

	}

	public static void doSerialize(Car myCar){
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(new File("myCar.file"));
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(myCar);
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Uuups, something went wrong: " + e.getLocalizedMessage());
		}

	}

	public static Car doDeSerialize(String filename){
		Car myCar = null;
		try {
			FileInputStream fis = new FileInputStream(new File(filename));
			ObjectInputStream ois = new ObjectInputStream(fis);
			myCar = (Car) ois.readObject();
			ois.close();
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
			System.out.println("Uuups, something went wrong: " + e.getLocalizedMessage());
		}

		return myCar;
	}
}

The Output of this example is:

Serialize the car to file myCar.file
Serialization done
Your car is: Porsche - 911 Carrera S - 400hourspower - 20000Euro
Deserialization done...

And a file „myCar.file“ was created in your working directory.