-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
180 lines (140 loc) · 6.63 KB
/
Solution.java
File metadata and controls
180 lines (140 loc) · 6.63 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//Jesus answered, "My Kingdom is not of this world. If my Kingdom were of this world, then my servants would fight, that I wouldn't be delivered to the Jews. But now my Kingdom is not from here." (John 18:36)
package com.javarush.task.task20.task2004;
import java.io.*;
import java.util.Properties;
/*
Читаем и пишем в файл статики
*/
public class Solution {
public static void main(String[] args) {
try {
File your_file_name = new File("E:\\5.txt");
OutputStream outputStream = new FileOutputStream(your_file_name);
InputStream inputStream = new FileInputStream(your_file_name);
ClassWithStatic classWithStatic = new ClassWithStatic();
classWithStatic.i = 3;
classWithStatic.j = 4;
classWithStatic.save(outputStream);
outputStream.flush();
ClassWithStatic loadedObject = new ClassWithStatic();
loadedObject.staticString = "something";
loadedObject.i = 6;
loadedObject.j = 7;
loadedObject.load(inputStream);
outputStream.close();
inputStream.close();
System.out.println(ClassWithStatic.staticString);
System.out.println(loadedObject.i);
System.out.println(loadedObject.j);
} catch (IOException e) {
System.out.println("Oops, something wrong with my file");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Oops, something wrong with save/load method");
}
}
public static class ClassWithStatic {
public static String staticString = "it's test static string";
public int i;
public int j;
public void save(OutputStream outputStream) throws Exception {
PrintWriter printWriter = new PrintWriter(outputStream);
printWriter.println(staticString);
printWriter.println(i);
printWriter.println(j);
printWriter.close();
}
public void load(InputStream inputStream) throws Exception {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
ClassWithStatic.staticString = bufferedReader.readLine();
this.i = Integer.parseInt(bufferedReader.readLine());
this.j = Integer.parseInt(bufferedReader.readLine());
bufferedReader.close();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClassWithStatic that = (ClassWithStatic) o;
if (i != that.i) return false;
return j == that.j;
}
@Override
public int hashCode() {
int result = i;
result = 31 * result + j;
return result;
}
}
/*
Читаем и пишем в файл статики
Реализуй логику записи в файл и чтения из файла для класса ClassWithStatic.
Метод load должен инициализировать объект включая статические поля данными из файла.
Метод main не участвует в тестировании.
Требования:
1. Должна быть реализована возможность сохранения/загрузки объектов типа Solution.ClassWithStatic с помощью методов save/load.
2. Класс Solution не должен поддерживать интерфейс Serializable.
3. Класс Solution.ClassWithStatic не должен поддерживать интерфейс Serializable.
4. Класс Solution.ClassWithStatic должен быть публичным.
5. Класс Solution.ClassWithStatic не должен поддерживать интерфейс Externalizable.
package com.javarush.task.task20.task2004;
import java.io.*;
*
Читаем и пишем в файл статики
*
public class Solution {
public static void main(String[] args) {
//you can find your_file_name.tmp in your TMP directory or fix outputStream/inputStream according to your real file location
//вы можете найти your_file_name.tmp в папке TMP или исправьте outputStream/inputStream в соответствии с путем к вашему реальному файлу
try {
File your_file_name = File.createTempFile("your_file_name", null);
OutputStream outputStream = new FileOutputStream(your_file_name);
InputStream inputStream = new FileInputStream(your_file_name);
ClassWithStatic classWithStatic = new ClassWithStatic();
classWithStatic.i = 3;
classWithStatic.j = 4;
classWithStatic.save(outputStream);
outputStream.flush();
ClassWithStatic loadedObject = new ClassWithStatic();
loadedObject.staticString = "something";
loadedObject.i = 6;
loadedObject.j = 7;
loadedObject.load(inputStream);
//check here that classWithStatic object equals to loadedObject object - проверьте тут, что classWithStatic и loadedObject равны
outputStream.close();
inputStream.close();
} catch (IOException e) {
//e.printStackTrace();
System.out.println("Oops, something wrong with my file");
} catch (Exception e) {
//e.printStackTrace();
System.out.println("Oops, something wrong with save/load method");
}
}
public static class ClassWithStatic {
public static String staticString = "it's test static string";
public int i;
public int j;
public void save(OutputStream outputStream) throws Exception {
//implement this method - реализуйте этот метод
}
public void load(InputStream inputStream) throws Exception {
//implement this method - реализуйте этот метод
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClassWithStatic that = (ClassWithStatic) o;
if (i != that.i) return false;
return j == that.j;
}
@Override
public int hashCode() {
int result = i;
result = 31 * result + j;
return result;
}
}
}
*/