Reading/Writing Text Files
Learn to read and write text files using Files and BufferedReader/BufferedWriter.
Reading/Writing Text Files is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Reading and writing text is common. Java provides Files, BufferedReader, and BufferedWriter for these tasks.
Files.readAllLines
Files.readAllLines() reads all lines into a List<String>. Great for small files, but not for very large ones.
Code: readAllLines
This code prints all lines of example.txt. Use it only if the file size is manageable.
public class Main {
public static void main(String[] args) throws java.io.IOException {
java.nio.file.Path p = java.nio.file.Paths.get("example.txt");
java.util.List<String> lines = java.nio.file.Files.readAllLines(p);
for (String line : lines) {
System.out.println(line);
}
}
}
BufferedReader intro
BufferedReader is efficient for line-by-line reading. It avoids loading the whole file at once.
Code: BufferedReader
This code reads a file line by line with BufferedReader inside try-with-resources.
public class Main {
public static void main(String[] args) {
java.nio.file.Path p = java.nio.file.Paths.get("example.txt");
try (java.io.BufferedReader br = java.nio.file.Files.newBufferedReader(p)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (java.io.IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Code: BufferedWriter
BufferedWriter writes text efficiently with methods like write() and newLine().
public class Main {
public static void main(String[] args) {
java.nio.file.Path p = java.nio.file.Paths.get("output.txt");
try (java.io.BufferedWriter bw = java.nio.file.Files.newBufferedWriter(p)) {
bw.write("Hello");
bw.newLine();
bw.write("World");
} catch (java.io.IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
BufferedReader check
Quick check: Which class is commonly used for efficient line-by-line reading?
Recap
Recap: Files.readAllLines is simple, BufferedReader handles big files line by line, and BufferedWriter writes text with ease.
Frequently asked questions
Is the “Reading/Writing Text Files” lesson free?
Yes — the full text of “Reading/Writing Text Files” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading/Writing Text Files”?
Learn to read and write text files using Files and BufferedReader/BufferedWriter. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Reading/Writing Text Files” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- java.nio.file Path/Files
- Reading/Writing Text Files
- Buffered Streams