record.txtの内容
5643 一二三四 2001年4月10日
4543 一五三四 2000年12月2日
・
・
・
という感じの半角スペースで区切られた内容のデータを読み書きするプログラム
java
1import java.util.ArrayList;
2import java.io.*;
3import java.util.*;
4public class DR{
5
6static ArrayList<Point> q=new ArrayList<>();
7
8
9public static void main(String[] args){
10
11
12try(BufferedReader br=new BufferedReader(new FileReader("C:\\file\\Java\\record.txt"));){
13
14String data=null;
15
16while((data=br.readLine())!=null){
17
18String[] data2=data.split(" ",0);
19
20Point p=new Point(data2[0],data2[1],data2[2]);
21
22q.add(p);
23
24}
25
26
27}catch(Exception e){
28
29
30}
31
32
33
34
35
36int flag=0;
37
38
39while(flag==0){
40
41Console console=System.console();
42
43System.out.println("1:社員情報の追加 2:社員情報の個別表示 3:一覧 4:保存 5:終了");
44
45int nu=0;
46
47
48int flag2=0;
49while(flag2==0){
50
51
52String name=console.readLine("番号:");
53
54try{
55
56nu=Integer.parseInt(name);
57flag2=1;
58}catch(Exception e){}
59
60
61}
62
63switch(nu){
64
65
66
67case 1:
68
69
70int nu2;
71
72
73String name1=console.readLine("社員番号:");
74
75String name2=console.readLine("氏名:");
76
77String name3=console.readLine("生年月日 年:");
78String name4=console.readLine(" 月:");
79String name5=console.readLine(" 日:");
80
81
82String name6=name3+"年"+name4+"月"+name5+"日";
83
84Point t=new Point(name1,name2,name6);
85
86q.add(t);
87
88
89
90break;
91
92case 2:
93
94String name7=console.readLine("社員番号:");
95
96for(int a=0;a<q.size();a++){
97
98if((q.get(a).num).equals(name7)){
99
100System.out.print("社員番号:");
101System.out.println(q.get(a).num);
102System.out.print("氏名:");
103System.out.println(q.get(a).name);
104System.out.print("生年月日:");
105System.out.println(q.get(a).birth);
106
107}
108
109
110}
111
112break;
113case 3:
114
115for(int a=0;a<q.size();a++){
116
117System.out.print("社員番号:");
118System.out.println(q.get(a).num);
119System.out.print("氏名:");
120System.out.println(q.get(a).name);
121System.out.print("生年月日:");
122System.out.println(q.get(a).birth);
123
124System.out.println();
125
126}
127
128
129break;
130
131
132case 4:
133
134try(BufferedWriter bw=new BufferedWriter(new FileWriter("C:\\file\\Java\\record.txt"));){
135
136for(Point c:q){
137
138String as=c.num+" "+c.name+" "+c.birth;
139
140bw.write(as);
141bw.newLine();
142
143}
144
145
146
147}catch(Exception e){}
148
149
150
151
152
153case 5:
154flag=1;
155break;
156default:
157flag=1;
158break;
159
160
161
162
163}
164
165}
166
167
168}
169
170
171
172}
173
174
175class Point{
176
177public Point(String a,String b,String c){
178
179
180
181
182num=a;
183name=b;
184birth=c;
185
186}
187
188String num;
189String name;
190String birth;
191
192
193
194
195
196}