-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMemoNote.java
More file actions
154 lines (121 loc) · 3.57 KB
/
MemoNote.java
File metadata and controls
154 lines (121 loc) · 3.57 KB
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
package ch12;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class MemoNote {
Scanner scanner;
File saveDir;
File[] newList;
public MemoNote() {
scanner = new Scanner(System.in);
saveDir = new File("d:\\javamemo");
}
public void viewMemoList() {
if (!saveDir.exists()) {
System.out.println("저장 폴더가 존재하지 않아 폴더를 생성합니다.");
saveDir.mkdir();
} else {
newList = saveDir.listFiles();
System.out.println("메모 리스트 ================================");
if (newList.length > 0) {
int cnt = 0;
for (File file : newList) {
if (file.isDirectory()) {
// System.out.print("[DIR] ");
} else if (file.isFile()) {
// System.out.print("[FILE] ");
}
System.out.println("[" + ++cnt + "] " + file.getName());
}
} else {
System.out.println("저장 폴더에 파일이 존재하지 않습니다.");
}
System.out.println("=======================================");
}
}
public void writeMemo() {
BufferedWriter out = null;
try {
System.out.print("제목을 입력해주세요.");
String subject = scanner.nextLine();
System.out.println();
System.out.print("메모내용을 입력해주세요.");
String memo = scanner.nextLine();
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-dd");
String writeDate = format.format(now);
System.out.println(writeDate);
String newFileName = writeDate + "_" + subject;
out = new BufferedWriter(new FileWriter(new File(saveDir, newFileName + ".txt")));
System.out.println(subject);
System.out.println(memo);
out.write(subject);
out.newLine();
out.newLine();
out.write(writeDate);
out.newLine();
out.newLine();
out.write(memo);
out.close();
System.out.println("Memo 입력 완료.");
} catch (IOException e) {
e.printStackTrace();
}
}
public void viewMemo() {
viewMemoList();
System.out.println("파일의 번호를 입력해주세요");
int select = Integer.parseInt(scanner.nextLine());
String fileName = newList[select].getName();
File viewFile = new File(saveDir, fileName);
BufferedReader in = null;
String str = null;
System.out.println("파일 읽기 ========================");
try {
in = new BufferedReader(new FileReader(viewFile));
while (true) {
str = in.readLine();
if (str == null)
break;
System.out.println(str);
}
System.out.println("===============================");
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MemoNote memoNote = new MemoNote();
while (true) {
System.out.println("JAVA Memo =====================================");
System.out.println(" [1] 메모 목록 보기 [2] 메모 등록 [3] 메모 보기 [4] 종료");
System.out.println("===============================================");
System.out.println("원하시는 기능의 번호를 입력하세요.");
int select = Integer.parseInt(memoNote.scanner.nextLine());
switch (select) {
case 1:
memoNote.viewMemoList();
break;
case 2:
memoNote.writeMemo();
break;
case 3:
memoNote.viewMemo();
break;
case 4:
System.out.println("프로그램을 종료합니다.");
return;
}
}
}
}