I have a problem, I don't know how to read two objects from a single file in Java.
This is my code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package prova.file;
/**
*
* @author Stefano
*/
import java.io.*;
import java.util.*;
public class ProvaFile implements Serializable {
/**
* @param args the command line arguments
*/
public static void main(String[] args)throws ClassNotFoundException {
// TODO code application logic here
objectone a=new objectone();
objecttwo b=new objecttwo();
objectone aa=null;
objecttwo bb=null;
Scanner m=new Scanner(System.in);
System.out.println("inserisci per scegliere");
int c=m.nextInt();
switch (c){
case 1: try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.dat"));
oos.writeObject(a);
oos.close();
}catch(Exception ex){
System.out.println("Serialization Save Error : "+ex.getMessage());
}
break;
case 2: try{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data.dat", true));
oos.writeObject(b);
oos.reset();
oos.flush();
oos.close();
}catch(Exception ex){
System.out.println("Serialization save error: "+ex.getMessage());
}
break;
case 3:try{
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("data.dat"));
aa=(objectone)ois.readObject();
bb=(objecttwo)ois.readObject();
ois.close();
}catch(IOException ex){
System.out.println(ex.getMessage());
}
aa.getS();
bb.getS();
break;
}
}
}
I don't know if my fault is in the write or in the read, can you give me a little help?