-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
117 lines (90 loc) · 4.47 KB
/
Solution.java
File metadata and controls
117 lines (90 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//Now it was the Preparation Day of the Passover, at about the sixth hour. He said to the Jews, "Behold, your King!" (John 19:14)
package com.javarush.task.task20.task2022;
import java.io.*;
/*
Переопределение сериализации в потоке
*/
public class Solution implements Serializable, AutoCloseable {
transient private FileOutputStream stream;
private 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();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
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 IOException, ClassNotFoundException {
Solution solution = new Solution("e:\\shalom.txt");
solution.writeObject("Shalom!\r\n");
ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("e:\\solution.obj"));
oos.writeObject(solution);
ObjectInputStream ois = new ObjectInputStream((new FileInputStream("e:\\solution.obj")));
Solution solution2 = (Solution) ois.readObject();
solution2.writeObject("Shalom again!");
}
}
/*
Переопределение сериализации в потоке
Сериализация/десериализация Solution не работает.
Исправь ошибки не меняя сигнатуры методов и класса.
Метод main не участвует в тестировании.
Написать код проверки самостоятельно в методе main:
1) создать экземпляр класса Solution
2) записать в него данные — writeObject
3) сериализовать класс Solution — writeObject(ObjectOutputStream out)
4) десериализовать, получаем новый объект
5) записать в новый объект данные — writeObject
6) проверить, что в файле есть данные из п.2 и п.5
Требования:
1. Поле stream должно быть объявлено с модификатором transient.
2. В методе writeObject(ObjectOutputStream out) не должен быть вызван метод close у потока полученного в качестве параметра.
3. В методе readObject(ObjectInputStream in) не должен быть вызван метод close у потока полученного в качестве параметра.
4. В методе readObject(ObjectInputStream in) поле stream должно быть инициализировано новым объектом типа FileOutputStream с параметрами(fileName, true).
5. В конструкторе класса Solution поле stream должно быть инициализировано новым объектом типа FileOutputStream с параметром(fileName).
package com.javarush.task.task20.task2022;
import java.io.*;
*
Переопределение сериализации в потоке
*
public class Solution implements Serializable, AutoCloseable {
private 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 IOException {
out.defaultWriteObject();
out.close();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
in.close();
}
@Override
public void close() throws Exception {
System.out.println("Closing everything!");
stream.close();
}
public static void main(String[] args) {
}
}
*/