Java Writing a File
Open Store.java and write letters into note.txt with FileWriter. Put tea and a line break into the file, then close the writer. Compile with javac Store.java and run java Store. The screen stays empty — the letters go into the file, not onto the terminal. A second demo writes oak the same way and replaces whatever was already inside. Leave the Paths topic for the next page; this lesson only stores short text with FileWriter.
Store tea and keep the screen empty
The reading page opened a file that already held oak. This page creates or replaces that text from the program itself. Open a teaching file named Store.java. Keep the public class name Store so the file name matches. Delete leftover Reader scanner demos, leftover Signal throw lines, leftover Guard catch blocks, and any keyboard Scanner on System.in. The program must not wait for typing. This page needs import lines for writing, not for reading.
Near the top, keep:
import java.io.FileWriter;
import java.io.IOException;
Mark main with throws IOException so a failed write can leave main as that checked exception. Inside main, open the disk name for writing:
FileWriter out = new FileWriter("note.txt");
Write one short line including the line break:
out.write("tea\n");
Then finish the store:
out.close();
Do not call System.out.println in this first teaching file. The empty screen is part of the lesson: writing is not the same as printing.
import java.io.FileWriter;
import java.io.IOException;
public class Store {
public static void main(String[] args) throws IOException {
FileWriter out = new FileWriter("note.txt");
out.write("tea\n");
out.close();
}
}
From the folder that should hold note.txt after the run, execute javac Store.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Store — not java Store.java. Leave the .java ending off the run command. If javac cannot find FileWriter, the import line is missing or mistyped — put it back before you chase other errors.
The terminal stays empty. That is correct. Open note.txt in an editor after the run and you should see tea on the first line. If the terminal prints oak or waits for keyboard input, you are still launching a Reader leftover — switch to java Store. If you see empty from an IllegalArgumentException, a Signal throw leftover is still active. If you see blocked or after, a Guard catch leftover is still the class you are launching.
close finishes the store. Before close, do not count the line as safely saved. After close, the letters stay in note.txt when the program ends. Mode talk from other languages is not needed here: this FileWriter constructor opens the name for writing and replaces older content when that name already exists. Creating note.txt when it was missing is part of that same open-for-write behavior. You do not call Scanner or nextLine while storing; those belong to reading.
Walk the run once. Java starts at Store.main. FileWriter opens note.txt for writing. write places tea and the line break. close ends the writer. Nothing is printed. If you skip close and stop the program early, the file may look incomplete — keep close in the teaching file. Run the program from the folder where you want note.txt to appear, so the simple name resolves next to your class file.
A later write replaces older letters
A second program starts fresh with the same shape. It still uses FileWriter on note.txt, but the text is oak instead of tea.
import java.io.FileWriter;
import java.io.IOException;
public class Store {
public static void main(String[] args) throws IOException {
FileWriter out = new FileWriter("note.txt");
out.write("oak\n");
out.close();
}
}
Compile with javac Store.java, then run java Store. The screen stays empty again. After this run, note.txt holds oak, not tea. The older line was replaced. That is the contrast with appending tools you are not using on this page — this constructor write starts the file content over for the teaching story. Spelling of the file name must match what you intend to open later: note.txt, not a mistyped twin sitting beside it.
You can confirm with the previous page’s Reader demo if you still have it: reading note.txt after the oak write should print oak. You do not need to paste Reader into Store.java. Keep writing and reading as separate teaching files so each lesson stays clear. One program writes; another program reads; that split keeps each screen honest.
Do not invent a Paths API on this page. You are writing a simple name string "note.txt" next to the class file. The next page is about paths as their own topic. Also do not treat a missing folder as today’s puzzle — keep the working directory the folder where you compile and run.
Compared with reading on Reader
On the files page, java Reader printed a line that was already on disk, or stopped with FileNotFoundException for a missing name. On this page, java Store puts the letters there itself. Reading and writing stay different jobs. Keep Reader files nearby only as history when you want to check what landed; run java Store for the write demos.
FileWriter and write are the new tokens. close still matters, just as Scanner.close mattered when reading. Skipping close is a common leftover mistake after a successful compile.
What this leaves for paths
This page stores short text with a simple file name beside your class. The next page introduces paths more carefully. Stay with FileWriter, write, and close here. Do not add path objects as a requirement on these Store demos yet.
Before paths
You can write tea into note.txt and see an empty screen while the file holds that line. You can write oak next and replace the older text. Compile Store.java and run java Store for each kept demo in turn. Open the text file in an editor when you want to confirm what landed. The next page is paths, where names and locations get their own focus.
Keep the tea write for a clear first store. Keep the oak write for a clear replace. Keep path APIs off this page. Those three habits are the checkpoint before you move on. When note.txt shows oak after the second java Store run, writing a file did its job for this lesson and you are ready for the next topic about paths.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top