Вроде всё работает. Валидатор не пропускает по п.3...
package com.javarush.task.task20.task2022;
import java.io.*;
/*
Переопределение
сериализации в потоке
*/
public class Solution
implements Serializable,
AutoCloseable {
/*myPtint:
public static
void pl(Object o) {
System.out.println(o);
}
//------*/
private
static final long
serialVersionUID = 1L;
// 1
transient private
FileOutputStream stream;
private String filename;
//public Solution(){}
public Solution(
String fileName) throws
FileNotFoundException {
this.filename =
fileName;
/*
pl("ctor Solution "+
"fileName\n: "+
fileName);
*/
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 IOException {
out.
defaultWriteObject();
/*
pl("\nwrite fileName="+
filename);
*/
out.writeObject(
filename);
}
private void
readObject(
ObjectInputStream in)
throws IOException,
ClassNotFoundException {
in.defaultReadObject();
filename = (String)in.
readObject();
/*
pl("\nreadObject fileName="+
filename);
*/
stream = new
FileOutputStream(
filename,true);
}
@Override
public void close()
throws Exception {
System.out.println(
"Closing everything!");
stream.close();
}
public static void main(
String[] args) throws
FileNotFoundException,
Exception {
// pl("begin main:\n");
try(
Solution s =
new Solution(args[1]);
//pl("\nnew Solution");
/*
pl("\nmain Soluion."+
"filename: "+
s.stream);
*/
//save:
FileOutputStream
fos = new
FileOutputStream(
args[2] );
ObjectOutputStream
oos = new
ObjectOutputStream(
fos);
//load
FileInputStream
fis = new
FileInputStream(
args[2] );
ObjectInputStream
ois = new
ObjectInputStream(
fis);
) {
/*
s.writeObject(
"mister");
*/
s.writeObject(
"Привет!!!");
//save
oos.writeObject(s);
oos.flush();
//load
Solution s2 =
(Solution)ois.
readObject();
//ois.close();
/*
pl("\n nain s2.filename:"+
s2.filename);
*/
s2.writeObject(
"hello!!!");
}
catch(Exception e) {
// pl("Ecxeption!!!");
// e.printStackTrace();
}
// oos.close();
//ois.close();
}
}