try {
//init
TestClass mb = new TestClass("private string", 1111);
mb.publicString = "public string";
mb.publicInt = 123;
//write
StringBuffer str = new StringBuffer();
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
str.append((char)b);
}
};
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(mb);
oos.close();
//read
int i = 0;
InputStream in = new InputStream() {
@Override
public int read() throws IOException {
return str.charAt(i);
}
};
ObjectInputStream ois = new ObjectInputStream(in);
TestClass result = (TestClass) ois.readObject();
ois.close();
System.out.println(result.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Получаю ошибку
java.io.StreamCorruptedException: invalid stream header: ACACACAC
на строке:
ObjectInputStream ois = new ObjectInputStream(in);
Возможно, я зря использую с потоки и это можно сделать проще