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.
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.
This program introduces the syntax for BufferedWriter
. We create a FileWriter
and pass an example path to it. This is the target text file.
write()
on the BufferedWriter
. No newline is inserted afterwards. We can pass a string
argument.NewLine()
inserts a platform-specific newline to the file. Often we call this after a string
to create a file with lines.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
string
arrayThis 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
BufferedWriter
handles more than strings. It can write just a part of a string
—a substring (specified with a start index and length).
char
, we must use the append method. The write method will not work—it treats chars as integers.char
array. We can specify just a range within a 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 closedOften the BufferedWriter
will throw exceptions. In this example, we get an IOException
because we write to a file we had already closed.
BufferedWriter
, causes many errors. Often exception handling (try, catch) keywords are needed.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)
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.