Code example for ObjectOutputStream

Methods: close, writeObject

0
	static DemoSingleton2 instanceOne = DemoSingleton2.getInstance();
 
	public static void main(String[] args) {
		try { 
			// Serialize to a file 
			ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
					"filename.ser")); 
			out.writeObject(instanceOne);
			out.close();
 
			instanceOne.setI(20);
 
			// Serialize to a file 
			ObjectInput in = new ObjectInputStream(new FileInputStream(
					"filename.ser")); 
			DemoSingleton2 instanceTwo = (DemoSingleton2) in.readObject();
			in.close();
 
			System.out.println(instanceOne.getI());
			System.out.println(instanceTwo.getI());