package oop.FileHandling.SerializationAndDeserialization;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test {

	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		
		File file = new File("C:\\Users\\obaya\\eclipse-workspace\\OOP\\src\\oop\\FileHandling\\SerializationAndDeserialization\\FileTest.txt");
		
		//(Serialization): schreibe das Objekt auf dem File als byte stream
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
		writeObjekt write = new writeObjekt(111,"obai",3.4);
		oos.writeObject(write);
		
		//(Deserialization): Lese das Objekt auf dem File als byte stream
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
		
		// Das zurückgegebene Objekt muss in den spezifischen Typ gecastet werden
		writeObjekt f1 = (writeObjekt) ois.readObject();
		System.out.println(f1);
		
		
		
		// multiple objects (lesen & schreiben)
		
		//schreiben:
		writeObjekt s1 = new writeObjekt(111,"Obai",2.2);
		writeObjekt s2 = new writeObjekt(111,"Omar",2.2);
		ArrayList<writeObjekt> list = new ArrayList<>();
		list.add(s1);
		list.add(s2);
		
		ObjectOutputStream writeList = new ObjectOutputStream(new FileOutputStream(file));
		writeList.writeObject(list);
		
		//Lesen:
		ObjectInputStream readList = new ObjectInputStream(new FileInputStream(file));
		ArrayList<writeObjekt> multiobjekt = (ArrayList<writeObjekt>)readList.readObject();
		System.out.println(multiobjekt);



	}

}
