C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Write: We call this method on the BufferedWriter. No newline is inserted afterwards. We can pass a string argument.
NewLine: This inserts a platform-specific newline to the file. Often we call this after a string to create a file with lines.
Close: This finishes the file writing operations. We cannot write any more to the file.
Java program that uses BufferedWriter, write, newLine
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();
}
}
Output file: output.txt
CAT
DOG
BIRD
Java program that writes String array to 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();
}
}
Output file: output.txt
100A
200B
300C
Char: To write a char, we must use the append method. The write method will not work—it treats chars as integers.
Int: With write we write integers. If we write the ASCII value 97 we see an "a" in the file.
Char array: Write can handle an entire char array. We can also specify just a range within a char array.
Char ArraysJava program that writes substrings, chars, ints
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();
}
}
Output file: output.txt
cd
b
a
abcdefghi
Warning: Writing files, as with BufferedWriter, causes many errors. Often exception handling (try, catch) keywords are needed.
ExceptionsJava program that writes after close
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");
}
}
Output
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)