teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

2020/04/19 10:55

投稿

TTTTkuma
TTTTkuma

スコア7

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

2020/04/19 10:55

投稿

TTTTkuma
TTTTkuma

スコア7

title CHANGED
@@ -1,1 +1,1 @@
1
- ンパイルエラの原因とコードをどう修正すれか教えて下さい。
1
+ コードをどう直せばいか教えて下さい。
body CHANGED
File without changes

2

2020/04/19 10:50

投稿

TTTTkuma
TTTTkuma

スコア7

title CHANGED
File without changes
body CHANGED
@@ -1,11 +1,10 @@
1
- javaで以下のココンパイルしようとするとコンパイルエラーが起き、Exception in thread "main" java.io.FileNotFoundException: D:\java\member.txtとでます。
1
+ StudentTestクラスにてテキストファイルstudent.txtをからデ読み込み、テキストファイルの各行をStudentオブジェクトに変換てバナリファイstudent.datに書込みそのStudentに書き込れたデータが正しいか確認したいのでが上手くいきません
2
- 何故こなるのでしょうか?
2
+ 改善すればよろしいでしょうか?助言くださると助かります。
3
3
  ```java
4
4
 
5
- package filetest;
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 FileTest {
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 (Exception e) {
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
- output.writeObject(new Date(1992, 3, 23));
58
+ output.writeObject(new Date(1992, 3, 23));
62
- output.writeObject(new Date(1997, 5, 3));
59
+ output.writeObject(new Date(1997, 5, 3));
63
- output.writeObject(new Date(2010, 7, 16));
60
+ output.writeObject(new Date(2010, 7, 16));
64
- output.writeObject("hello");
61
+ output.writeObject("hello");
65
- output.writeObject(new Member("John", new Date(1997, 5, 3)));
62
+ output.writeObject(new Student("John","213" , new Date(1997, 5, 3)));
66
- output.close();
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
- output.write("Abc");
70
+ output.write("Abc");
75
- output.write("物件導向");
71
+ output.write("オブジェクト指向");
76
- output.close();
72
+ }
77
73
  } catch (IOException ex) {
78
- Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
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
- Member members[] = new Member[3];
79
+ Student students[] = new Student[3];
84
- int index = 0;
80
+ int index = 0;
85
- while (true) {
81
+ while (true) {
86
- String line = input.readLine();
82
+ String line = input.readLine();
87
- if (line == null) {
83
+ if (line == null) {
88
- break;
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
- members[index] = new Member(name, new Date(yy, mm, dd));
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
- while (true) {
107
+ while (true) {
106
- String line = input.readLine();
108
+ String line = input.readLine();
107
- if (line == null) {
109
+ if (line == null) {
108
- break;
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
- while (reader.ready()) {
118
+ while (reader.ready()) {
117
-
119
+
118
- char[] chIn = new char[10];
120
+ char[] chIn = new char[10];
119
-
121
+
120
- int nResult = reader.read(chIn);
122
+ int nResult = reader.read(chIn);
121
- if (nResult == -1) {
123
+ if (nResult == -1) {
122
- break;
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
- System.out.println("input is " + fis);
136
+ System.out.println("input is " + fis);
134
- while (fis.available() > 0) {
137
+ while (fis.available() > 0) {
138
+
135
- byte[] b = new byte[1];
139
+ byte[] b = new byte[1];
140
+
136
- int nResult = fis.read(b);
141
+ int nResult = fis.read(b);
137
- if (nResult == -1) {
142
+ if (nResult == -1) {
138
- break;
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(FileTest.class.getName()).log(Level.SEVERE, null, ex);
150
+ Logger.getLogger(StudentTest.class.getName()).log(Level.SEVERE, null, ex);
146
151
  } catch (IOException ex) {
147
- Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
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 filetest;
163
+ package studenttest;
155
- import java.io.Serializable;
156
- public class Date implements Serializable {
164
+ public class Student {
165
+ String name;
166
+ String id;
157
- int year, month, day;
167
+ Date birthday;
158
- public Date(int yy, int mm, int dd) {
168
+ public Student(String n,String i, Date d) {
159
- year = yy;
169
+ name = n;
160
- month = mm;
170
+ id = i;
161
- day = dd;
171
+ birthday = d;
172
+
162
173
  }
174
+ @Override
163
175
  public String toString() {
164
- return String.format("%04d/%02d/%02d", year, month, day);
176
+ return name + ": " + id +": " + birthday;
165
177
  }
166
178
  }
167
179
 
168
180
  ```
169
181
  ```java
170
182
 
171
- package filetest;
183
+ package studenttest;
172
184
  import java.io.Serializable;
173
- public class Member implements Serializable {
185
+ public class Date implements Serializable {
174
- String name;
175
- Date birthday;
186
+ int year, month, day;
176
- public Member(String n, Date d) {
187
+ public Date(int yy, int mm, int dd) {
177
- name = n;
188
+ year = yy;
189
+ month = mm;
178
- birthday = d;
190
+ day = dd;
179
191
  }
180
192
  public String toString() {
181
- return name + ": " + birthday;
193
+ return String.format("%04d/%02d/%02d", year, month, day);
182
194
  }
183
195
  }
184
196
 

1

2020/04/19 10:50

投稿

TTTTkuma
TTTTkuma

スコア7

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
- chIn 中,若已達檔案底端則回傳-1
119
+
120
120
  int nResult = reader.read(chIn);
121
121
  if (nResult == -1) {
122
122
  break;