Home
Java
BufferedWriter Examples
Updated May 29, 2025
Dot Net Perls
BufferedWriter. This Java class helps us write Strings and characters to a file. We can write a String with write() and with newLine() we add a newline.
File
With BufferedWriter we are not limited to Strings. We can write chars, ints, arrays and even substrings. We often pass a FileWriter to the BufferedWriter constructor.
First example. This program introduces the syntax for BufferedWriter. We create a FileWriter and pass an example path to it. This is the target text file.
Note We call write() on the BufferedWriter. No newline is inserted afterwards. We can pass a string argument.
Next NewLine() inserts a platform-specific newline to the file. Often we call this after a string to create a file with lines.
Finally Close() finishes the file writing operations—we cannot write any more to the file.
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Program { public static void main(String[] args) throws IOException { // Create a BufferedWriter around a FileWriter. // ... Write to an output text file. BufferedWriter writer = new BufferedWriter(new FileWriter( "C:\\programs\\output.txt")); // Write these lines to the file. // ... We call newLine to insert a newline character. writer.write("CAT"); writer.newLine(); writer.write("DOG"); writer.newLine(); writer.write("BIRD"); writer.close(); } }
CAT DOG BIRD
Write string array. This example writes an entire String array to a file. It specifies a newline after each String. So we write an array to lines in a file.
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Program { public static void main(String[] args) throws IOException { String[] array = new String[3]; array[0] = "100A"; array[1] = "200B"; array[2] = "300C"; // Create our BufferedWriter. BufferedWriter writer = new BufferedWriter(new FileWriter( "C:\\programs\\output.txt")); // Loop over the elements in the string array and write each line. for (String line : array) { writer.write(line); writer.newLine(); } writer.close(); } }
100A 200B 300C
Substrings, chars, ints. BufferedWriter handles more than strings. It can write just a part of a string—a substring (specified with a start index and length).
Tip To write a char, we must use the append method. The write method will not work—it treats chars as integers.
Info With write we write integers. If we write the ASCII value 97 we see an "a" in the file.
Also Write can handle an entire char array. We can specify just a range within a char array.
char Array
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Program { public static void main(String[] args) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter( "C:\\programs\\output.txt")); // Write the second two characters of this string. String value = "abcdefghi"; writer.write(value, 2, 2); writer.newLine(); // Append a character to the file. writer.append(value.charAt(1)); writer.newLine(); // Write an integer to the file. writer.write(97); writer.newLine(); // Write a character array. // ... A range of characters can be instead written. char[] array = value.toCharArray(); writer.write(array); // Close our writer. writer.close(); } }
cd b a abcdefghi
IOException, stream closed. Often the BufferedWriter will throw exceptions. In this example, we get an IOException because we write to a file we had already closed.
Warning Writing files, as with BufferedWriter, causes many errors. Often exception handling (try, catch) keywords are needed.
try
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Program { public static void main(String[] args) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter( "C:\\programs\\output.txt")); // Close the writer. writer.close(); // This will cause an exception. writer.write("X"); } }
Exception in thread "main" java.io.IOException: Stream closed at java.io.BufferedWriter.ensureOpen(Unknown Source) at java.io.BufferedWriter.write(Unknown Source) at java.io.Writer.write(Unknown Source) at Program.main(Program.java:15)
Throws IOException. In the examples, we use the "throws IOException" notation on our main methods. This is required—BufferedReader can cause an error for many reasons.
With BufferedReader, and the FileWriter type as help, we write Strings to file with ease. We can form lines with the newLine method.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 29, 2025 (simplify).
Home
Changes
© 2007-2025 Sam Allen