-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.java
More file actions
55 lines (50 loc) · 1.9 KB
/
FileHandler.java
File metadata and controls
55 lines (50 loc) · 1.9 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
// #File read/write operations for data.txt.
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileHandler {
private final String filePath = "tasks.txt";
public FileHandler() {
File file = new File(filePath);
try {
if (file.createNewFile()) {
System.out.println("Task file created: " + file.getName());
}
} catch (IOException e) {
System.out.println("Error creating file: " + e.getMessage());
}
}
public void writeTasks(List<Task> tasks) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (Task task : tasks) {
writer.write(task.getId() + ";" + task.getTitle() + ";" + task.getDescription() + ";" +
task.getDueDate() + ";" + task.isCompleted());
writer.newLine();
}
} catch (IOException e) {
System.out.println("Error writing to file: " + e.getMessage());
}
}
public List<Task> readTasks() {
List<Task> tasks = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(";");
if (data.length == 5) {
Task task = new Task(
Integer.parseInt(data[0]),
data[1],
data[2],
data[3],
Boolean.parseBoolean(data[4])
);
tasks.add(task);
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
return tasks;
}
}