0

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 .

TNT
  • 2,900
  • 3
  • 23
  • 34
Isaac
  • 1
  • 3
  • did you use 2 different `ObjectOutputStream` to append data to the file? – Ramanlfc Feb 26 '16 at 19:25
  • no dude , i have an inteface with a new Kratisi form .user just complete the form and with a button the program append the data to file – Isaac Feb 27 '16 at 07:08
  • Let me rephrase . So i open the application ,i make 2 new entries and the app writes them to the file . When im trying to show them its fine . The problem appears when im trying to run the app a second time . It reads the objects fine , but if i write new object/objects it throws the streamcorrupted error . Is there any way to avoid this ?? – Isaac Feb 27 '16 at 07:56
  • Serialization does not produce text and should not be stored in `.txt` files. – user207421 Aug 25 '17 at 00:28

0 Answers0