forked from Shreyash220404/Java-File-I-O-Notes-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesManager.java
More file actions
59 lines (54 loc) · 2.01 KB
/
Copy pathNotesManager.java
File metadata and controls
59 lines (54 loc) · 2.01 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
import java.io.*;
import java.util.Scanner;
public class NotesManager {
private static final String FILE_NAME = "notes.txt";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nNotes Manager");
System.out.println("1. Add Note");
System.out.println("2. View Notes");
System.out.println("3. Exit");
System.out.print("Choose option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
addNote(scanner);
break;
case 2:
viewNotes();
break;
case 3:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid option.");
}
}
}
private static void addNote(Scanner scanner) {
System.out.print("Enter your note: ");
String note = scanner.nextLine();
try (FileWriter fw = new FileWriter(FILE_NAME, true)) {
fw.write(note + System.lineSeparator());
System.out.println("Note saved.");
} catch (IOException e) {
System.out.println("Error writing note: " + e.getMessage());
}
}
private static void viewNotes() {
System.out.println("\nYour Notes:");
try (BufferedReader br = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
int count = 1;
while ((line = br.readLine()) != null) {
System.out.println(count++ + ". " + line);
}
} catch (FileNotFoundException e) {
System.out.println("No notes found.");
} catch (IOException e) {
System.out.println("Error reading notes: " + e.getMessage());
}
}
}