Skip to content

Instantly share code, notes, and snippets.

@FaAway
Last active February 10, 2016 13:07
Show Gist options
  • Save FaAway/60afe5a0c9b2e80630da to your computer and use it in GitHub Desktop.
Save FaAway/60afe5a0c9b2e80630da to your computer and use it in GitHub Desktop.
javarush level20.lesson10.home07
package com.javarush.test.level20.lesson10.home07;
import java.io.*;
/* Переопределение сериализации в потоке
Сериализация/десериализация Solution не работает.
Исправьте ошибки не меняя сигнатуры методов и класса.
Метод main не участвует в тестировании.
Написать код проверки самостоятельно в методе main:
1) создать экземпляр класса Solution
2) записать в него данные - writeObject
3) сериализовать класс Solution - writeObject(ObjectOutputStream out)
4) десериализовать, получаем новый объект
5) записать в новый объект данные - writeObject
6) проверить, что в файле есть данные из п.2 и п.5
*/
public class Solution implements Serializable, AutoCloseable {
private String fileName;
private transient FileOutputStream stream;
public static void main(String[] args) throws IOException, ClassNotFoundException{
Solution sol = new Solution("2writeobject");
sol.writeObject("2 write object");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object"));
oos.writeObject(sol);
oos.flush();
oos.close();
ObjectInputStream oIS = new ObjectInputStream(new FileInputStream("object"));
Solution loadedSol = (Solution)oIS.readObject();
loadedSol.writeObject("5 write object");
}
public Solution(String fileName) throws FileNotFoundException {
this.fileName = 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();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
stream = new FileOutputStream(this.fileName, true);
}
@Override
public void close() throws Exception {
System.out.println("Closing everything!");
stream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment