Java · Lesson 115 of 120
Project: Saving Tasks
Write them to disk.
When the program ends, the tasks in memory disappear. TaskStore saves them to a text file, one task per line, so they're still there next time.
- Memory disappears
- Save to a file
- One task per line
TaskStore: save
TaskStore remembers the path of its file. Save turns each task into one line of text. The four values are separated by semicolons. The title goes last, so a semicolon inside a title can't confuse us later. Files dot write saves all the lines.
TaskStore.javajava
public class TaskStore {
private final Path file;
public TaskStore(String name) { file = Path.of(name); }
public void save(List<Task> tasks) throws IOException {
List<String> lines = new ArrayList<>();
for (Task t : tasks) {
lines.add(t.id() + ";" + t.priority() + ";" + t.done() + ";" + t.title());
}
Files.write(file, lines);
}
// load() comes next lesson
}Saving for real
We add two tasks, complete the first, and save. Then we print the file itself. Run it. Each line holds the id, the priority, done, and the title. Even the title with a semicolon is kept whole.
Main.javajava
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
TaskList list = new TaskList();
list.add("Pay rent", 3);
list.add("Book dentist; maybe Friday", 2);
list.complete(1);
new TaskStore("tasks.txt").save(list.all());
System.out.println("--- tasks.txt ---");
System.out.print(Files.readString(Path.of("tasks.txt")));
}
}Output
--- tasks.txt --- 1;3;true;Pay rent 2;2;false;Book dentist; maybe Friday
Also uses Task.java
public record Task(int id, String title, int priority, boolean done) {
public Task {
if (title.isBlank()) throw new IllegalArgumentException("Title can't be empty");
if (priority < 1 || priority > 3) throw new IllegalArgumentException("1-3 only");
}
public Task complete() {
return new Task(id, title, priority, true);
}
@Override
public String toString() {
String box = done ? "[x]" : "[ ]";
return String.format("%2d %s %-3s %s", id, box, "!".repeat(priority), title);
}
}Also uses TaskList.java
import java.util.*;
public class TaskList {
private final List<Task> tasks = new ArrayList<>();
private int nextId = 1;
public Task add(String title, int priority) {
Task t = new Task(nextId++, title, priority, false);
tasks.add(t);
return t;
}
public void complete(int id) {
tasks.replaceAll(t -> t.id() == id ? t.complete() : t);
}
public boolean remove(int id) { return tasks.removeIf(t -> t.id() == id); }
public List<Task> all() { return List.copyOf(tasks); }
}Also uses TaskStore.java
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
public class TaskStore {
private final Path file;
public TaskStore(String name) { file = Path.of(name); }
public void save(List<Task> tasks) throws IOException {
List<String> lines = new ArrayList<>();
for (Task t : tasks) {
lines.add(t.id() + ";" + t.priority() + ";" + t.done() + ";" + t.title());
}
Files.write(file, lines);
}
public List<Task> load() throws IOException {
List<Task> out = new ArrayList<>();
if (!Files.exists(file)) return out;
for (String line : Files.readAllLines(file)) {
String[] p = line.split(";", 4);
int id = Integer.parseInt(p[0]), prio = Integer.parseInt(p[1]);
out.add(new Task(id, p[3], prio, Boolean.parseBoolean(p[2])));
}
return out;
}
}Summary
Save writes each task as a line, with semicolons between the values, and the title last. Next, loading them back.
Key takeaways- id;priority;done;title
- Title last
- Files.write
Next up: Project: Loading Tasks — read them back