подскажите плз, почему close закрывает поток который принимаем в качестве параметра? ведь у нас метод переопределен : @Override public void close() throws Exception { System.out.println("Closing everything!"); stream.close(); } и тут видно что close вызывается именно у stream, а не у ObjectOutputStream out или ObjectInputStream in. Спасибо! ниже решенная задача: public class Solution implements Serializable, AutoCloseable { private transient FileOutputStream stream; String filename; public Solution(String fileName) throws FileNotFoundException { this.stream = new FileOutputStream(fileName); this.filename = fileName; } public void writeObject(String string) throws IOException { stream.write(string.getBytes()); stream.write("\n".getBytes()); stream.flush(); } private void writeObject(ObjectOutputStream out) throws IOException { out.defaultWriteObject(); // out.close(); } //т.к. мы сделали транзиентной записью поток, надо при чтени этот поток инициализировать!!! private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); this.stream = new FileOutputStream(this.filename,true); // in.close(); } @Override public void close() throws Exception { System.out.println("Closing everything!"); stream.close(); } public static void main(String[] args) { } }