質問編集履歴
4
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
StudentTestクラスにてテキストファイルstudent.txtをからデータを読み込み、テキストファイルの各行をStudentオブジェクトに変換してバイナリファイルstudent.datに書き込み、そのStudentに書き込まれたデータが正しいか確認したいのですが上手くいきません。
|
2
2
|
どう改善すればよろしいでしょうか?助言くださると助かります。
|
3
|
+
テキストファイル内の文書は
|
4
|
+
|
5
|
+
Sam 001 1999/01/07
|
6
|
+
Jennifer 002 1998/04/05
|
7
|
+
Bob 003 1999/03/14
|
8
|
+
Jonathan 004 1998/09/07
|
9
|
+
となります。
|
10
|
+
|
3
11
|
```java
|
4
12
|
|
5
13
|
package studenttest;
|
3
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
コ
|
1
|
+
コードをどう直せばいいか教えて下さい。
|
body
CHANGED
File without changes
|
2
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
1
|
+
StudentTestクラスにてテキストファイルstudent.txtをからデータを読み込み、テキストファイルの各行をStudentオブジェクトに変換してバイナリファイルstudent.datに書き込み、そのStudentに書き込まれたデータが正しいか確認したいのですが上手くいきません。
|
2
|
-
|
2
|
+
どう改善すればよろしいでしょうか?助言くださると助かります。
|
3
3
|
```java
|
4
4
|
|
5
|
-
package
|
5
|
+
package studenttest;
|
6
6
|
import java.io.BufferedReader;
|
7
7
|
import java.io.BufferedWriter;
|
8
|
-
import java.io.DataInputStream;
|
9
8
|
import java.io.File;
|
10
9
|
import java.io.FileInputStream;
|
11
10
|
import java.io.FileNotFoundException;
|
@@ -18,7 +17,7 @@
|
|
18
17
|
import java.io.ObjectOutputStream;
|
19
18
|
import java.util.logging.Level;
|
20
19
|
import java.util.logging.Logger;
|
21
|
-
public class
|
20
|
+
public class StudentTest {
|
22
21
|
|
23
22
|
private static void processAllFiles(File dir) {
|
24
23
|
File files[] = dir.listFiles();
|
@@ -33,7 +32,8 @@
|
|
33
32
|
}
|
34
33
|
public static void main(String[] args) throws IOException, ClassNotFoundException {
|
35
34
|
|
36
|
-
|
35
|
+
readStudents("D:\students.txt","big5");
|
36
|
+
|
37
37
|
}
|
38
38
|
|
39
39
|
|
@@ -41,7 +41,6 @@
|
|
41
41
|
ObjectInputStream input=null;
|
42
42
|
try {
|
43
43
|
input = new ObjectInputStream(new FileInputStream(filename));
|
44
|
-
|
45
44
|
while (true) {
|
46
45
|
Object o = input.readObject();
|
47
46
|
System.out.println(o);
|
@@ -50,135 +49,148 @@
|
|
50
49
|
}
|
51
50
|
}
|
52
51
|
input.close();
|
53
|
-
} catch (
|
52
|
+
} catch (IOException | ClassNotFoundException e) {
|
54
|
-
|
55
53
|
input.close();
|
56
54
|
}
|
57
55
|
}
|
58
56
|
private static void writeObjectStream(String filename) throws FileNotFoundException, IOException {
|
59
|
-
|
60
|
-
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(filename))
|
57
|
+
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(filename))) {
|
61
|
-
|
58
|
+
output.writeObject(new Date(1992, 3, 23));
|
62
|
-
|
59
|
+
output.writeObject(new Date(1997, 5, 3));
|
63
|
-
|
60
|
+
output.writeObject(new Date(2010, 7, 16));
|
64
|
-
|
61
|
+
output.writeObject("hello");
|
65
|
-
|
62
|
+
output.writeObject(new Student("John","213" , new Date(1997, 5, 3)));
|
66
|
-
|
63
|
+
}
|
67
64
|
}
|
68
65
|
private static void writeToFileWithEncoding(String fileanme, String encoding) {
|
69
66
|
try {
|
70
67
|
FileOutputStream stream = new FileOutputStream(fileanme);
|
71
68
|
OutputStreamWriter writer = new OutputStreamWriter(stream, encoding);
|
72
|
-
BufferedWriter output = new BufferedWriter(writer)
|
69
|
+
try (BufferedWriter output = new BufferedWriter(writer)) {
|
73
|
-
|
74
|
-
|
70
|
+
output.write("Abc");
|
75
|
-
|
71
|
+
output.write("オブジェクト指向");
|
76
|
-
|
72
|
+
}
|
77
73
|
} catch (IOException ex) {
|
78
|
-
Logger.getLogger(
|
74
|
+
Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex);
|
79
75
|
}
|
80
76
|
}
|
81
77
|
private static void readMembers(String filename, String encoding) throws FileNotFoundException, IOException {
|
82
|
-
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding))
|
78
|
+
try (BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding))) {
|
83
|
-
|
79
|
+
Student students[] = new Student[3];
|
84
|
-
|
80
|
+
int index = 0;
|
85
|
-
|
81
|
+
while (true) {
|
86
|
-
|
82
|
+
String line = input.readLine();
|
87
|
-
|
83
|
+
if (line == null) {
|
88
|
-
|
84
|
+
break;
|
85
|
+
}
|
86
|
+
|
87
|
+
String tokens[] = line.split(",");
|
88
|
+
|
89
|
+
String name = tokens[0], id = tokens[1], birthday = tokens[2];
|
90
|
+
String births[] = birthday.split("/");
|
91
|
+
|
92
|
+
int yy = Integer.parseInt(births[0]), mm=Integer.parseInt(births[1]),dd=Integer.parseInt(births[2]);
|
93
|
+
|
94
|
+
students[index] = new Student(name, id, new Date(yy, mm, dd));
|
95
|
+
index++;
|
96
|
+
|
89
97
|
}
|
90
|
-
|
98
|
+
System.out.println("--------------");
|
91
|
-
|
99
|
+
for(Student member : students) System.out.println(member);
|
92
|
-
index++;
|
93
|
-
|
94
100
|
}
|
95
|
-
System.out.println("--------------");
|
96
|
-
for(Member member : members) System.out.println(member);
|
97
|
-
input.close();
|
98
101
|
}
|
99
102
|
|
100
103
|
private static void readByReaderWithEncoding(String filename, String encoding) throws FileNotFoundException, IOException {
|
101
104
|
FileInputStream stream = new FileInputStream(filename);
|
102
105
|
InputStreamReader reader = new InputStreamReader(stream, encoding);
|
103
|
-
BufferedReader input = new BufferedReader(reader)
|
106
|
+
try (BufferedReader input = new BufferedReader(reader)) {
|
104
|
-
BufferedReader(new InputStreamReader(new FileInputStream(filename), encoding));
|
105
|
-
|
107
|
+
while (true) {
|
106
|
-
|
108
|
+
String line = input.readLine();
|
107
|
-
|
109
|
+
if (line == null) {
|
108
|
-
|
110
|
+
break;
|
111
|
+
}
|
112
|
+
System.out.println(line);
|
109
113
|
}
|
110
|
-
System.out.println(line);
|
111
114
|
}
|
112
|
-
input.close();
|
113
115
|
}
|
114
116
|
private static void readByReaderDefaultEncoding(String filename) throws FileNotFoundException, IOException {
|
115
|
-
FileReader reader = new FileReader(filename)
|
117
|
+
try (FileReader reader = new FileReader(filename)) {
|
116
|
-
|
118
|
+
while (reader.ready()) {
|
117
|
-
|
119
|
+
|
118
|
-
|
120
|
+
char[] chIn = new char[10];
|
119
|
-
|
121
|
+
|
120
|
-
|
122
|
+
int nResult = reader.read(chIn);
|
121
|
-
|
123
|
+
if (nResult == -1) {
|
122
|
-
|
124
|
+
break;
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
System.out.println(chIn);
|
123
129
|
}
|
124
|
-
|
125
|
-
System.out.println(chIn);
|
126
130
|
}
|
127
|
-
reader.close();
|
128
131
|
}
|
129
132
|
private static void readFileStream(String filename) {
|
130
133
|
try {
|
131
134
|
System.out.println("main started");
|
132
|
-
FileInputStream fis = new FileInputStream(filename)
|
135
|
+
try (FileInputStream fis = new FileInputStream(filename)) {
|
133
|
-
|
136
|
+
System.out.println("input is " + fis);
|
134
|
-
|
137
|
+
while (fis.available() > 0) {
|
138
|
+
|
135
|
-
|
139
|
+
byte[] b = new byte[1];
|
140
|
+
|
136
|
-
|
141
|
+
int nResult = fis.read(b);
|
137
|
-
|
142
|
+
if (nResult == -1) {
|
138
|
-
|
143
|
+
break;
|
144
|
+
}
|
145
|
+
|
146
|
+
System.out.print(new String(b));
|
139
147
|
}
|
140
|
-
|
141
|
-
System.out.print(new String(b));
|
142
148
|
}
|
143
|
-
fis.close();
|
144
149
|
} catch (FileNotFoundException ex) {
|
145
|
-
Logger.getLogger(
|
150
|
+
Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex);
|
146
151
|
} catch (IOException ex) {
|
147
|
-
Logger.getLogger(
|
152
|
+
Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex);
|
148
153
|
}
|
149
154
|
}
|
155
|
+
private static void readStudents(String dstudentstxt, String big5) {
|
156
|
+
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
157
|
+
}
|
158
|
+
}
|
150
159
|
|
151
160
|
```
|
152
161
|
```java
|
153
162
|
|
154
|
-
package
|
163
|
+
package studenttest;
|
155
|
-
import java.io.Serializable;
|
156
|
-
public class
|
164
|
+
public class Student {
|
165
|
+
String name;
|
166
|
+
String id;
|
157
|
-
|
167
|
+
Date birthday;
|
158
|
-
public
|
168
|
+
public Student(String n,String i, Date d) {
|
159
|
-
|
169
|
+
name = n;
|
160
|
-
|
170
|
+
id = i;
|
161
|
-
|
171
|
+
birthday = d;
|
172
|
+
|
162
173
|
}
|
174
|
+
@Override
|
163
175
|
public String toString() {
|
164
|
-
return
|
176
|
+
return name + ": " + id +": " + birthday;
|
165
177
|
}
|
166
178
|
}
|
167
179
|
|
168
180
|
```
|
169
181
|
```java
|
170
182
|
|
171
|
-
package
|
183
|
+
package studenttest;
|
172
184
|
import java.io.Serializable;
|
173
|
-
public class
|
185
|
+
public class Date implements Serializable {
|
174
|
-
String name;
|
175
|
-
|
186
|
+
int year, month, day;
|
176
|
-
public
|
187
|
+
public Date(int yy, int mm, int dd) {
|
177
|
-
|
188
|
+
year = yy;
|
189
|
+
month = mm;
|
178
|
-
|
190
|
+
day = dd;
|
179
191
|
}
|
180
192
|
public String toString() {
|
181
|
-
return
|
193
|
+
return String.format("%04d/%02d/%02d", year, month, day);
|
182
194
|
}
|
183
195
|
}
|
184
196
|
|
1
title
CHANGED
File without changes
|
body
CHANGED
@@ -116,7 +116,7 @@
|
|
116
116
|
while (reader.ready()) {
|
117
117
|
|
118
118
|
char[] chIn = new char[10];
|
119
|
-
|
119
|
+
|
120
120
|
int nResult = reader.read(chIn);
|
121
121
|
if (nResult == -1) {
|
122
122
|
break;
|