質問編集履歴

1

追加

2016/02/15 08:37

投稿

akagami_bb
akagami_bb

スコア19

test CHANGED
File without changes
test CHANGED
@@ -21,3 +21,333 @@
21
21
  他の場所でArrayListの<Person>を使用していますので、<Person>を<String>に書き換えずに、追加する方法はないのでしょうか?
22
22
 
23
23
  ご教授の方よろしく御願い致します。
24
+
25
+
26
+
27
+ 説明不足で申し訳ございません。
28
+
29
+ 少しでも不備がないよう下記に全部掲載させていただきます。
30
+
31
+
32
+
33
+ import java.io.BufferedReader;
34
+
35
+ import java.io.BufferedWriter;
36
+
37
+ import java.io.File;
38
+
39
+ import java.io.FileReader;
40
+
41
+ import java.io.FileWriter;
42
+
43
+ import java.io.IOException;
44
+
45
+ import java.io.PrintWriter;
46
+
47
+ import java.util.ArrayList;
48
+
49
+
50
+
51
+ public class Text {
52
+
53
+ // 1人分のデータを格納するクラス
54
+
55
+ class Person {
56
+
57
+ private int num;
58
+
59
+ private String name;
60
+
61
+ private String address;
62
+
63
+ private String tel;
64
+
65
+
66
+
67
+ public int getNum(){
68
+
69
+ return num;
70
+
71
+ }
72
+
73
+ public void setNum(int num){
74
+
75
+ this.num = num;
76
+
77
+ }
78
+
79
+ public String getName() {
80
+
81
+ return name;
82
+
83
+ }
84
+
85
+ public void setName(String name) {
86
+
87
+ this.name = name;
88
+
89
+ }
90
+
91
+ public String getAddress() {
92
+
93
+ return address;
94
+
95
+ }
96
+
97
+ public void setAddress(String address) {
98
+
99
+ this.address = address;
100
+
101
+ }
102
+
103
+ public String getTel() {
104
+
105
+ return tel;
106
+
107
+ }
108
+
109
+ public void setTel(String tel) {
110
+
111
+ this.tel = tel;
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ private boolean checkBeforeReadFile(File file) {
120
+
121
+ if (file.exists()) {
122
+
123
+ if (file.isFile() && file.canRead()) {
124
+
125
+ return true;
126
+
127
+ }
128
+
129
+ }
130
+
131
+ return false;
132
+
133
+ }
134
+
135
+
136
+
137
+
138
+
139
+ // ファイルを読み込む
140
+
141
+ public ArrayList<Person> readFile(File file) {
142
+
143
+ int i = 0;
144
+
145
+ int j =1;
146
+
147
+ ArrayList<Person> list = new ArrayList<Person>();
148
+
149
+ BufferedReader br = null;
150
+
151
+ try {
152
+
153
+ if (checkBeforeReadFile(file)) {
154
+
155
+ br = new BufferedReader(new FileReader(file));
156
+
157
+ String line = null;
158
+
159
+ Person person = null;
160
+
161
+
162
+
163
+ while ((line = br.readLine()) != null) {
164
+
165
+ switch (i) {
166
+
167
+ case 0:
168
+
169
+ // Person クラスのインスタンスを生成
170
+
171
+ person = new Person();
172
+
173
+ // 名前を格納
174
+
175
+ person.setName(line);
176
+
177
+ // 次は i = 1 → 住所
178
+
179
+ i++;
180
+
181
+ person.setNum(j);
182
+
183
+ j++;
184
+
185
+ break;
186
+
187
+ case 1:
188
+
189
+ person.setAddress(line);
190
+
191
+ i++;
192
+
193
+ break;
194
+
195
+ case 2:
196
+
197
+ person.setTel(line);
198
+
199
+ // 1人分のデータの格納が完了したので、リストに追加
200
+
201
+ list.add(person);
202
+
203
+ i = 0;
204
+
205
+ break;
206
+
207
+ }
208
+
209
+ }
210
+
211
+ }
212
+
213
+ }
214
+
215
+ catch (IOException e) {
216
+
217
+ e.printStackTrace();
218
+
219
+ }
220
+
221
+ finally {
222
+
223
+ if (br != null) {
224
+
225
+ try {
226
+
227
+ br.close();
228
+
229
+ }
230
+
231
+ catch (IOException e) {
232
+
233
+ e.printStackTrace();
234
+
235
+ }
236
+
237
+ }
238
+
239
+ }
240
+
241
+ return list;
242
+
243
+ }
244
+
245
+
246
+
247
+ // 全員分のデータを表示する
248
+
249
+ void showPeople(ArrayList<Person> list) {
250
+
251
+ for (Person person : list) {
252
+
253
+ System.out.println("<No." + person.getNum() + ">");
254
+
255
+ System.out.println("名前:" + person.getName());
256
+
257
+ System.out.println("住所:" + person.getAddress());
258
+
259
+ System.out.println("電話番号:" + person.getTel());
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+
267
+ //コンソール制御
268
+
269
+ public static void main(String[] args) {
270
+
271
+ try{
272
+
273
+ File file = new File("test.txt");
274
+
275
+
276
+
277
+ BufferedReader br = new BufferedReader(new FileReader(file));
278
+
279
+ Text text = new Text();
280
+
281
+ ArrayList<Person> list = text.readFile(file);
282
+
283
+ text.showPeople(list);
284
+
285
+ System.out.println("0:新規登録 1:削除");
286
+
287
+ int input = new java.util.Scanner(System.in).nextInt();
288
+
289
+
290
+
291
+
292
+
293
+ if(input == 0){
294
+
295
+ PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
296
+
297
+ System.out.println("名前を入力してください");
298
+
299
+ String name = new java.util.Scanner(System.in).nextLine();
300
+
301
+ System.out.println("住所を入力してください");
302
+
303
+ String address = new java.util.Scanner(System.in).nextLine();
304
+
305
+ System.out.println("電話番号を入力してください");
306
+
307
+ String tel = new java.util.Scanner(System.in).nextLine();
308
+
309
+ //ここでエラー
310
+
311
+ list.add(name);
312
+
313
+ list.add(address);
314
+
315
+ list.add(tel);
316
+
317
+ /*pw.println("\n");*/
318
+
319
+ pw.close();
320
+
321
+ System.out.println("連絡先を追加しました");
322
+
323
+ }
324
+
325
+
326
+
327
+
328
+
329
+ if(input == 1){
330
+
331
+ System.out.println("削除したい番号");
332
+
333
+ int del = new java.util.Scanner(System.in).nextInt();
334
+
335
+ list.remove(del);
336
+
337
+ System.out.println("No." + del + "のデータを削除しました");
338
+
339
+ }
340
+
341
+ }
342
+
343
+ catch(IOException e){
344
+
345
+ System.out.println(e);
346
+
347
+ }
348
+
349
+ }
350
+
351
+
352
+
353
+ }