프로그램을 실행하면, 파일을 읽고(없으면 예외처리) 지정한 형식에 맞게 문자열을 나누어 Vector<Person>에 자동으로 집어넣어서 프로그램이 원활하게 작동하도록 하였다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
package _1103A;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;
public class Main {
public static Scanner Keyboard = new Scanner(System.in);
public static Vector<Person> PersonData = new Vector<Person>();
private static String FileName = "PersonData.txt";
public static void main(String[] args) {
LoadTextData();
while (true) {
DisplayMenu();
switch (Keyboard.nextInt()) {
case 1: {
InputData();
System.out.println();
break;
}
case 2: {
DisplayAllData();
System.out.println();
break;
}
case 3: {
System.out.println("프로그램을 종료합니다.");
return;
}
case 4: {
RemoveFile();
System.out.println();
break;
}
}
}
}
public static void RemoveFile() {
File file = new File(FileName);
if (file.exists()) {
if (file.delete()) {
System.out.println("파일삭제 성공");
PersonData.clear();
} else {
System.out.println("파일삭제 실패");
}
} else {
System.out.println("파일이 존재하지 않습니다.");
}
}
public static void LoadTextData() {
BufferedReader in = null;
StringBuilder str = new StringBuilder();
try {
in = new BufferedReader(new FileReader(FileName));
String line;
while (true) {
line = in.readLine();
if (line == null)
break;
str.append(line);
}
InputDataFromFileStream(str);
} catch (FileNotFoundException e) {
System.out.println("파일이 없습니다!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void InputDataFromFileStream(StringBuilder fileStream) {
StringBuilder strName = new StringBuilder();
StringBuilder strAge = new StringBuilder();
for (int i = 0; i < fileStream.length(); ++i) {
if (fileStream.charAt(i) == '{') {
++i;
strName.setLength(0);
strAge.setLength(0);
for (int j = i; j < fileStream.length(); ++j, ++i) {
if (fileStream.charAt(i) == ',') {
++j;
++i;
break;
}
strName.append(fileStream.charAt(i));
}
for (int j = i; j < fileStream.length(); ++j, ++i) {
if (fileStream.charAt(i) == '}') {
break;
}
strAge.append(fileStream.charAt(i));
}
PersonData.add(new Person(strName.toString(), Integer.parseInt(strAge.toString())));
}
}
}
public static void InputData() {
Keyboard.nextLine();
BufferedWriter out = null;
String nameInput;
int ageInput;
try {
out = new BufferedWriter(new FileWriter(FileName, true));
System.out.print("이름: ");
nameInput = Keyboard.nextLine();
System.out.print("나이: ");
ageInput = Keyboard.nextInt();
PersonData.add(new Person(nameInput, ageInput));
out.write('{' + nameInput + ',' + ageInput + "}, ");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void DisplayAllData() {
if (PersonData.size() == 0) {
System.out.println("데이터가 없습니다.");
return;
}
for (Person person : PersonData) {
person.DisplayInfo();
}
}
public static void DisplayMenu() {
System.out.println("== 메뉴 ==");
System.out.println("1. 입력");
System.out.println("2. 출력");
System.out.println("3. 종료");
System.out.println("4. 파일 제거");
System.out.print("무엇을 하시겠습니까? ");
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package _1103A;
public class Person {
private String mName;
private int mAge;
public Person(String name, int age)
{
this.mName = name;
this.mAge = age;
}
public void DisplayInfo()
{
System.out.println("이름: " + this.mName);
System.out.println("나이: " + this.mAge);
}
}
|
cs |
'기타 > univ. projects' 카테고리의 다른 글
[유니티] 게임엔진응용실습 Unit6 프로젝트 - Zombie Run (FPS 게임) (0) | 2022.11.19 |
---|---|
[유니티] 게임엔진응용실습 Unit5 프로젝트 - Tower Defense (Windows & Android) (0) | 2022.11.05 |
[자바] 멀티미디어자바프로젝트II ch.07 연습문제 (0) | 2022.10.27 |
[자바] 멀티미디어자바프로젝트II ch.06 연습문제 (0) | 2022.10.22 |
[유니티] 게임엔진응용실습 Unit4 프로젝트 - Airplane Shooting (Space Invader) (0) | 2022.10.22 |