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