Part 07 / Topic 6 of 7
Putting it together: a to-do list saved to a text file
Real-world example: a to-do list that two separate runs of the program share, by saving to tasks.txt after every change. This is the part's first "you will build" item, and it uses everything so far: two classes in one package (todoapp), compiled together, persisted with java.nio.file.
todoapp/Task.java:
package todoapp;
public class Task {
private final String description;
private boolean done;
public Task(String description, boolean done) {
this.description = description;
this.done = done;
}
public String getDescription() {
return description;
}
public boolean isDone() {
return done;
}
public void markDone() {
this.done = true;
}
// One line of the save file: "1|Buy milk" or "0|Call Ravi"
public String toLine() {
return (done ? "1" : "0") + "|" + description;
}
public static Task fromLine(String line) {
String[] parts = line.split("\\|", 2);
boolean isDone = parts[0].equals("1");
return new Task(parts[1], isDone);
}
@Override
public String toString() {
return (done ? "[x] " : "[ ] ") + description;
}
}
todoapp/TodoApp.java (the rest is argument parsing you already know from Part 2 (String[] args) and Part 3 (arrow switch); load/save are the only new pieces):
package todoapp;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TodoApp {
private static final Path STORE = Path.of("tasks.txt");
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("Usage: add <text> | list | done <number> | remove <number>");
return;
}
List<Task> tasks = load();
String command = args[0];
switch (command) {
case "add" -> {
String text = String.join(" ", Arrays.asList(args).subList(1, args.length));
tasks.add(new Task(text, false));
save(tasks);
System.out.println("Added: " + text);
}
case "list" -> printList(tasks);
case "done" -> {
int index = Integer.parseInt(args[1]) - 1;
tasks.get(index).markDone();
save(tasks);
System.out.println("Marked done: " + tasks.get(index).getDescription());
}
case "remove" -> {
int index = Integer.parseInt(args[1]) - 1;
Task removed = tasks.remove(index);
save(tasks);
System.out.println("Removed: " + removed.getDescription());
}
default -> System.out.println("Unknown command: " + command);
}
}
private static void printList(List<Task> tasks) {
if (tasks.isEmpty()) {
System.out.println("No tasks yet.");
return;
}
for (int i = 0; i < tasks.size(); i++) {
System.out.println((i + 1) + ". " + tasks.get(i));
}
}
private static List<Task> load() throws IOException {
if (!Files.exists(STORE)) {
return new ArrayList<>();
}
List<Task> tasks = new ArrayList<>();
for (String line : Files.readAllLines(STORE)) {
if (!line.isBlank()) {
tasks.add(Task.fromLine(line));
}
}
return tasks;
}
private static void save(List<Task> tasks) throws IOException {
List<String> lines = new ArrayList<>();
for (Task task : tasks) {
lines.add(task.toLine());
}
Files.write(STORE, lines);
}
}
Each command is a separate run of the JVM — load() reads whatever is on disk (an empty list the very first time), the command changes the in-memory list, and save() writes it straight back out. Compile once, then run several times:
$ javac -d out todoapp/Task.java todoapp/TodoApp.java
$ java -cp out todoapp.TodoApp add "Buy milk from the Pune store"
Added: Buy milk from the Pune store
$ java -cp out todoapp.TodoApp add "Call Ravi about the delivery"
Added: Call Ravi about the delivery
$ java -cp out todoapp.TodoApp add "Pay electricity bill"
Added: Pay electricity bill
$ java -cp out todoapp.TodoApp list
1. [ ] Buy milk from the Pune store
2. [ ] Call Ravi about the delivery
3. [ ] Pay electricity bill
$ java -cp out todoapp.TodoApp done 2
Marked done: Call Ravi about the delivery
$ java -cp out todoapp.TodoApp list
1. [ ] Buy milk from the Pune store
2. [x] Call Ravi about the delivery
3. [ ] Pay electricity bill
$ java -cp out todoapp.TodoApp remove 1
Removed: Buy milk from the Pune store
$ java -cp out todoapp.TodoApp list
1. [x] Call Ravi about the delivery
2. [ ] Pay electricity bill
tasks.txt on disk, after all of that:
$ cat tasks.txt
1|Call Ravi about the delivery
0|Pay electricity bill
Nothing was kept in memory between runs — every list you saw printed was rebuilt from this one small text file. That is the whole point of file persistence: the program can stop and restart (or even run on a different day) and the to-do list is still there.
Full program: examples/todo-app/.
Done here? The practice set for Part 7 — including the solutions — is on the part page.