I make a small registration I want all accounts to be stored in one file (each account is a new object). I can not until the end of the work principle of writeStreamHeader () and I can not understand how to implement it correctly so that my new object is added to the file with the created objects.

public static void saveAccaunt(LoginAndPass gamers){ try { File file = new File("test"); ObjectOutputStream os1 = new ObjectOutputStream(new FileOutputStream(file)); os1.writeObject(gamers); os1.close(); ObjectOutputStream os2 = new ObjectOutputStream(new FileOutputStream(file,true)){ @Override protected void writeStreamHeader() throws IOException { reset(); } }; os2.writeObject(gamers); os2.close(); } catch (IOException e) { e.printStackTrace(); } } 

With this code, only two objects are saved, with this code already writes an error:

 try { FileInputStream fileInputStream = new FileInputStream("SaveAcc"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); System.out.println(objectInputStream.readObject()); System.out.println(objectInputStream.readObject()); System.out.println(objectInputStream.readObject()); System.out.println(objectInputStream.readObject()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 

Error code: java.io.EOFException

  • EOFException is the same exception when you want to read after the end of the file, if I'm not mistaken. It is logical that you can read exactly as much as you have written desrialized objects. Check back. - Sckoriy

0