-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteManager.java
More file actions
46 lines (41 loc) · 1.41 KB
/
NoteManager.java
File metadata and controls
46 lines (41 loc) · 1.41 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
import java.io.*;
import java.util.*;
public class NoteManager {
private static final String NOTES_DIR = "notes";
public NoteManager() {
File dir = new File(NOTES_DIR);
if (!dir.exists()) {
dir.mkdir();
}
}
public void saveNote(String title, String content) throws IOException {
if (title == null || title.trim().isEmpty()) return;
File file = new File(NOTES_DIR, title + ".txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(content);
}
}
public String loadNote(String title) throws IOException {
File file = new File(NOTES_DIR, title + ".txt");
if (!file.exists()) return "";
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
}
return sb.toString();
}
public List<String> listNotes() {
File dir = new File(NOTES_DIR);
String[] files = dir.list((d, name) -> name.endsWith(".txt"));
List<String> titles = new ArrayList<>();
if (files != null) {
for (String f : files) {
titles.add(f.substring(0, f.length() - 4));
}
}
return titles;
}
}