-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.java
More file actions
38 lines (33 loc) · 1.29 KB
/
FileManager.java
File metadata and controls
38 lines (33 loc) · 1.29 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
import java.io.*;
import java.time.LocalDate;
import java.util.*;
public class FileManager {
public static void saveSessions(List<StudySession> sessions) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("sessions.txt"))) {
for (StudySession s : sessions) {
writer.write(s.getId() + "," + s.getSubject() + "," + s.getDurationSeconds() + "," + s.getDate());
writer.newLine();
}
} catch (IOException e) {
System.out.println("Error saving data.");
}
}
public static List<StudySession> loadSessions() {
List<StudySession> sessions = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("sessions.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
sessions.add(new StudySession(
Integer.parseInt(parts[0]),
parts[1],
Integer.parseInt(parts[2]), // seconds
LocalDate.parse(parts[3])
));
}
} catch (IOException e) {
System.out.println("No previous data found.");
}
return sessions;
}
}