質問編集履歴
4
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,6 +2,22 @@
|
|
2
2
|
|
3
3
|
どう改善すればよろしいでしょうか?助言くださると助かります。
|
4
4
|
|
5
|
+
テキストファイル内の文書は
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
Sam 001 1999/01/07
|
10
|
+
|
11
|
+
Jennifer 002 1998/04/05
|
12
|
+
|
13
|
+
Bob 003 1999/03/14
|
14
|
+
|
15
|
+
Jonathan 004 1998/09/07
|
16
|
+
|
17
|
+
となります。
|
18
|
+
|
19
|
+
|
20
|
+
|
5
21
|
```java
|
6
22
|
|
7
23
|
|
3
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
コ
|
1
|
+
コードをどう直せばいいか教えて下さい。
|
test
CHANGED
File without changes
|
2
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,19 +1,17 @@
|
|
1
|
-
|
1
|
+
StudentTestクラスにてテキストファイルstudent.txtをからデータを読み込み、テキストファイルの各行をStudentオブジェクトに変換してバイナリファイルstudent.datに書き込み、そのStudentに書き込まれたデータが正しいか確認したいのですが上手くいきません。
|
2
|
-
|
2
|
+
|
3
|
-
|
3
|
+
どう改善すればよろしいでしょうか?助言くださると助かります。
|
4
4
|
|
5
5
|
```java
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
package
|
9
|
+
package studenttest;
|
10
10
|
|
11
11
|
import java.io.BufferedReader;
|
12
12
|
|
13
13
|
import java.io.BufferedWriter;
|
14
14
|
|
15
|
-
import java.io.DataInputStream;
|
16
|
-
|
17
15
|
import java.io.File;
|
18
16
|
|
19
17
|
import java.io.FileInputStream;
|
@@ -38,7 +36,7 @@
|
|
38
36
|
|
39
37
|
import java.util.logging.Logger;
|
40
38
|
|
41
|
-
public class
|
39
|
+
public class StudentTest {
|
42
40
|
|
43
41
|
|
44
42
|
|
@@ -68,7 +66,9 @@
|
|
68
66
|
|
69
67
|
|
70
68
|
|
71
|
-
|
69
|
+
readStudents("D:\students.txt","big5");
|
70
|
+
|
71
|
+
|
72
72
|
|
73
73
|
}
|
74
74
|
|
@@ -84,8 +84,6 @@
|
|
84
84
|
|
85
85
|
input = new ObjectInputStream(new FileInputStream(filename));
|
86
86
|
|
87
|
-
|
88
|
-
|
89
87
|
while (true) {
|
90
88
|
|
91
89
|
Object o = input.readObject();
|
@@ -102,9 +100,7 @@
|
|
102
100
|
|
103
101
|
input.close();
|
104
102
|
|
105
|
-
} catch (Exception e) {
|
103
|
+
} catch (IOException | ClassNotFoundException e) {
|
106
|
-
|
107
|
-
|
108
104
|
|
109
105
|
input.close();
|
110
106
|
|
@@ -114,21 +110,19 @@
|
|
114
110
|
|
115
111
|
private static void writeObjectStream(String filename) throws FileNotFoundException, IOException {
|
116
112
|
|
117
|
-
|
118
|
-
|
119
|
-
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(filename))
|
113
|
+
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(filename))) {
|
120
|
-
|
114
|
+
|
121
|
-
output.writeObject(new Date(1992, 3, 23));
|
115
|
+
output.writeObject(new Date(1992, 3, 23));
|
122
|
-
|
116
|
+
|
123
|
-
output.writeObject(new Date(1997, 5, 3));
|
117
|
+
output.writeObject(new Date(1997, 5, 3));
|
124
|
-
|
118
|
+
|
125
|
-
output.writeObject(new Date(2010, 7, 16));
|
119
|
+
output.writeObject(new Date(2010, 7, 16));
|
126
|
-
|
120
|
+
|
127
|
-
output.writeObject("hello");
|
121
|
+
output.writeObject("hello");
|
128
|
-
|
122
|
+
|
129
|
-
output.writeObject(new
|
123
|
+
output.writeObject(new Student("John","213" , new Date(1997, 5, 3)));
|
130
|
-
|
124
|
+
|
131
|
-
|
125
|
+
}
|
132
126
|
|
133
127
|
}
|
134
128
|
|
@@ -140,19 +134,17 @@
|
|
140
134
|
|
141
135
|
OutputStreamWriter writer = new OutputStreamWriter(stream, encoding);
|
142
136
|
|
143
|
-
BufferedWriter output = new BufferedWriter(writer)
|
137
|
+
try (BufferedWriter output = new BufferedWriter(writer)) {
|
144
|
-
|
145
|
-
|
146
|
-
|
138
|
+
|
147
|
-
output.write("Abc");
|
139
|
+
output.write("Abc");
|
148
|
-
|
140
|
+
|
149
|
-
output.write("
|
141
|
+
output.write("オブジェクト指向");
|
150
|
-
|
142
|
+
|
151
|
-
|
143
|
+
}
|
152
144
|
|
153
145
|
} catch (IOException ex) {
|
154
146
|
|
155
|
-
Logger.getLogger(
|
147
|
+
Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex);
|
156
148
|
|
157
149
|
}
|
158
150
|
|
@@ -160,37 +152,51 @@
|
|
160
152
|
|
161
153
|
private static void readMembers(String filename, String encoding) throws FileNotFoundException, IOException {
|
162
154
|
|
163
|
-
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding))
|
155
|
+
try (BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding))) {
|
164
|
-
|
156
|
+
|
165
|
-
|
157
|
+
Student students[] = new Student[3];
|
166
|
-
|
158
|
+
|
167
|
-
int index = 0;
|
159
|
+
int index = 0;
|
168
|
-
|
160
|
+
|
169
|
-
while (true) {
|
161
|
+
while (true) {
|
170
|
-
|
162
|
+
|
171
|
-
String line = input.readLine();
|
163
|
+
String line = input.readLine();
|
172
|
-
|
164
|
+
|
173
|
-
if (line == null) {
|
165
|
+
if (line == null) {
|
174
|
-
|
166
|
+
|
175
|
-
break;
|
167
|
+
break;
|
176
|
-
|
168
|
+
|
177
|
-
}
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
178
|
-
|
173
|
+
String tokens[] = line.split(",");
|
174
|
+
|
175
|
+
|
176
|
+
|
179
|
-
|
177
|
+
String name = tokens[0], id = tokens[1], birthday = tokens[2];
|
178
|
+
|
180
|
-
|
179
|
+
String births[] = birthday.split("/");
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
int yy = Integer.parseInt(births[0]), mm=Integer.parseInt(births[1]),dd=Integer.parseInt(births[2]);
|
184
|
+
|
185
|
+
|
186
|
+
|
181
|
-
|
187
|
+
students[index] = new Student(name, id, new Date(yy, mm, dd));
|
182
|
-
|
188
|
+
|
183
|
-
index++;
|
189
|
+
index++;
|
184
|
-
|
185
|
-
|
186
|
-
|
190
|
+
|
191
|
+
|
192
|
+
|
187
|
-
}
|
193
|
+
}
|
188
|
-
|
194
|
+
|
189
|
-
System.out.println("--------------");
|
195
|
+
System.out.println("--------------");
|
190
|
-
|
196
|
+
|
191
|
-
for(
|
197
|
+
for(Student member : students) System.out.println(member);
|
192
|
-
|
198
|
+
|
193
|
-
|
199
|
+
}
|
194
200
|
|
195
201
|
}
|
196
202
|
|
@@ -202,55 +208,55 @@
|
|
202
208
|
|
203
209
|
InputStreamReader reader = new InputStreamReader(stream, encoding);
|
204
210
|
|
205
|
-
BufferedReader input = new BufferedReader(reader)
|
211
|
+
try (BufferedReader input = new BufferedReader(reader)) {
|
206
|
-
|
207
|
-
|
212
|
+
|
208
|
-
|
209
|
-
while (true) {
|
213
|
+
while (true) {
|
210
|
-
|
214
|
+
|
211
|
-
String line = input.readLine();
|
215
|
+
String line = input.readLine();
|
212
|
-
|
216
|
+
|
213
|
-
if (line == null) {
|
217
|
+
if (line == null) {
|
214
|
-
|
218
|
+
|
215
|
-
break;
|
219
|
+
break;
|
216
|
-
|
220
|
+
|
217
|
-
}
|
221
|
+
}
|
218
|
-
|
222
|
+
|
219
|
-
System.out.println(line);
|
223
|
+
System.out.println(line);
|
220
|
-
|
224
|
+
|
221
|
-
}
|
225
|
+
}
|
222
|
-
|
226
|
+
|
223
|
-
|
227
|
+
}
|
224
228
|
|
225
229
|
}
|
226
230
|
|
227
231
|
private static void readByReaderDefaultEncoding(String filename) throws FileNotFoundException, IOException {
|
228
232
|
|
229
|
-
FileReader reader = new FileReader(filename)
|
233
|
+
try (FileReader reader = new FileReader(filename)) {
|
230
|
-
|
234
|
+
|
231
|
-
while (reader.ready()) {
|
235
|
+
while (reader.ready()) {
|
232
|
-
|
233
|
-
|
234
|
-
|
236
|
+
|
237
|
+
|
238
|
+
|
235
|
-
char[] chIn = new char[10];
|
239
|
+
char[] chIn = new char[10];
|
236
|
-
|
237
|
-
|
238
|
-
|
240
|
+
|
241
|
+
|
242
|
+
|
239
|
-
int nResult = reader.read(chIn);
|
243
|
+
int nResult = reader.read(chIn);
|
240
|
-
|
244
|
+
|
241
|
-
if (nResult == -1) {
|
245
|
+
if (nResult == -1) {
|
242
|
-
|
246
|
+
|
243
|
-
break;
|
247
|
+
break;
|
244
|
-
|
248
|
+
|
245
|
-
}
|
249
|
+
}
|
246
|
-
|
247
|
-
|
248
|
-
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
249
|
-
System.out.println(chIn);
|
255
|
+
System.out.println(chIn);
|
250
|
-
|
256
|
+
|
251
|
-
}
|
257
|
+
}
|
252
|
-
|
258
|
+
|
253
|
-
|
259
|
+
}
|
254
260
|
|
255
261
|
}
|
256
262
|
|
@@ -260,41 +266,53 @@
|
|
260
266
|
|
261
267
|
System.out.println("main started");
|
262
268
|
|
263
|
-
FileInputStream fis = new FileInputStream(filename)
|
269
|
+
try (FileInputStream fis = new FileInputStream(filename)) {
|
264
|
-
|
270
|
+
|
265
|
-
System.out.println("input is " + fis);
|
271
|
+
System.out.println("input is " + fis);
|
266
|
-
|
272
|
+
|
267
|
-
while (fis.available() > 0) {
|
273
|
+
while (fis.available() > 0) {
|
268
|
-
|
274
|
+
|
275
|
+
|
276
|
+
|
269
|
-
byte[] b = new byte[1];
|
277
|
+
byte[] b = new byte[1];
|
270
|
-
|
278
|
+
|
279
|
+
|
280
|
+
|
271
|
-
int nResult = fis.read(b);
|
281
|
+
int nResult = fis.read(b);
|
272
|
-
|
282
|
+
|
273
|
-
if (nResult == -1) {
|
283
|
+
if (nResult == -1) {
|
274
|
-
|
284
|
+
|
275
|
-
break;
|
285
|
+
break;
|
276
|
-
|
286
|
+
|
277
|
-
}
|
287
|
+
}
|
278
|
-
|
279
|
-
|
280
|
-
|
288
|
+
|
289
|
+
|
290
|
+
|
281
|
-
System.out.print(new String(b));
|
291
|
+
System.out.print(new String(b));
|
282
|
-
|
292
|
+
|
283
|
-
}
|
293
|
+
}
|
284
|
-
|
294
|
+
|
285
|
-
|
295
|
+
}
|
286
296
|
|
287
297
|
} catch (FileNotFoundException ex) {
|
288
298
|
|
289
|
-
Logger.getLogger(
|
299
|
+
Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex);
|
290
300
|
|
291
301
|
} catch (IOException ex) {
|
292
302
|
|
293
|
-
Logger.getLogger(
|
303
|
+
Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex);
|
294
|
-
|
304
|
+
|
295
|
-
}
|
305
|
+
}
|
296
|
-
|
306
|
+
|
297
|
-
}
|
307
|
+
}
|
308
|
+
|
309
|
+
private static void readStudents(String dstudentstxt, String big5) {
|
310
|
+
|
311
|
+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
298
316
|
|
299
317
|
|
300
318
|
|
@@ -304,7 +322,47 @@
|
|
304
322
|
|
305
323
|
|
306
324
|
|
307
|
-
package
|
325
|
+
package studenttest;
|
326
|
+
|
327
|
+
public class Student {
|
328
|
+
|
329
|
+
String name;
|
330
|
+
|
331
|
+
String id;
|
332
|
+
|
333
|
+
Date birthday;
|
334
|
+
|
335
|
+
public Student(String n,String i, Date d) {
|
336
|
+
|
337
|
+
name = n;
|
338
|
+
|
339
|
+
id = i;
|
340
|
+
|
341
|
+
birthday = d;
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
@Override
|
348
|
+
|
349
|
+
public String toString() {
|
350
|
+
|
351
|
+
return name + ": " + id +": " + birthday;
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
```
|
360
|
+
|
361
|
+
```java
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
package studenttest;
|
308
366
|
|
309
367
|
import java.io.Serializable;
|
310
368
|
|
@@ -333,37 +391,3 @@
|
|
333
391
|
|
334
392
|
|
335
393
|
```
|
336
|
-
|
337
|
-
```java
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
package filetest;
|
342
|
-
|
343
|
-
import java.io.Serializable;
|
344
|
-
|
345
|
-
public class Member implements Serializable {
|
346
|
-
|
347
|
-
String name;
|
348
|
-
|
349
|
-
Date birthday;
|
350
|
-
|
351
|
-
public Member(String n, Date d) {
|
352
|
-
|
353
|
-
name = n;
|
354
|
-
|
355
|
-
birthday = d;
|
356
|
-
|
357
|
-
}
|
358
|
-
|
359
|
-
public String toString() {
|
360
|
-
|
361
|
-
return name + ": " + birthday;
|
362
|
-
|
363
|
-
}
|
364
|
-
|
365
|
-
}
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
```
|
1
test
CHANGED
File without changes
|
test
CHANGED
@@ -234,7 +234,7 @@
|
|
234
234
|
|
235
235
|
char[] chIn = new char[10];
|
236
236
|
|
237
|
-
|
237
|
+
|
238
238
|
|
239
239
|
int nResult = reader.read(chIn);
|
240
240
|
|