Бред какой-то. Все условия выполнены, а валидатор ругается.
package com.javarush.task.task20.task2022;
import java.io.*;
import java.lang.reflect.Field;
/*
Переопределение сериализации в потоке
*/
public class Solution implements Serializable, AutoCloseable {
private transient FileOutputStream stream;
public Solution(String fileName) throws FileNotFoundException {
this.stream = new FileOutputStream(fileName);
}
public void writeObject(String string) throws IOException {
stream.write(string.getBytes());
stream.write("\n".getBytes());
stream.flush();
}
private void writeObject(ObjectOutputStream out) throws NoSuchFieldException, IllegalAccessException, IOException {
Class<?> fos = stream.getClass();
Field path = fos.getDeclaredField("path");
path.setAccessible(true);
out.writeUTF((String) path.get(stream));
path.setAccessible(false);
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
this.stream = new FileOutputStream(in.readUTF(), true);
}
@Override
public void close() throws Exception {
System.out.println("Closing everything!");
stream.close();
}
public static void main(String[] args) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\111.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\111.txt"));
Solution solution = new Solution("D:\\123.txt");
solution.writeObject("aaaaaaaaaa");
solution.close();
solution.writeObject(oos);
oos.close();
Solution solution1 = new Solution("null");
solution1.readObject(ois);
ois.close();
solution1.writeObject("bbbbbbbbbb");
solution1.close();
}
}