So, I am trying to read Objects named Kratisi and I always get an error. I manage to make it show 1 object but when I added one or two more the errors came back. The errors were:
StreamCorruptedException :invalid type code AC
Can someone help me? Here is my code:
My function:
private ArrayList<Kratisi> readKratiseis() throws FileNotFoundException,IOException,ClassNotFoundException
{
in=new ObjectInputStream(new FileInputStream(new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\kratiseis.txt")));
ArrayList<Kratisi> tmp_list = new ArrayList<Kratisi>();
Kratisi tmp_kratisi =null ;
while(true)
{
try
{
tmp_kratisi = (Kratisi)in.readObject();
if (tmp_kratisi!=null)
{
tmp_list.add(tmp_kratisi);
}
}catch(EOFException eof){return tmp_list;}
}
}
private void closeFile() throws IOException
{
in.close();
}
And the way to call it from my class:
try {
list_kratiseis=readKratiseis();
closeFile();
} catch (IOException ex) {
Logger.getLogger(AnazitisiInterface.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(AnazitisiInterface.class.getName()).log(Level.SEVERE, null, ex);
}
Note: I call the function from inside the constructor of my class, the ObjectReader is declared outside and before the constructor. Also, I don't want to write and read an entire ArrayList, I want read-write one by one Object.
Thank you .