
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - BufferedWriter write(String s) method
Description
The Java BufferedWriter write(String s) method writes an entire String to the output stream. The method writes the string characters to the buffer, and you can optionally flush or close the stream to ensure the content is written to the output destination (e.g., a file).
Declaration
Following is the declaration for java.io.BufferedWriter.write(String s) method:
public void write(String s)
Parameters
s − String to be written
Return Value
This method does not return any value.
Exception
IOException− if an I/O error occurs.
Example - Using write(String s) method
The following example shows the usage of Java BufferedWriter write(String s) method.
BufferedWriterDemo.java
package com.tutorialspoint; import java.io.BufferedWriter; import java.io.IOException; import java.io.StringWriter; public class BufferedWriterDemo { public static void main(String[] args) throws IOException { StringWriter sw = null; BufferedWriter bw = null; String str = "Hello World!"; try { // create string writer sw = new StringWriter(); //create buffered writer bw = new BufferedWriter(sw); // writing string to writer bw.write(str); // forces out the characters to string writer bw.flush(); // string buffer is created StringBuffer sb = sw.getBuffer(); //prints the string System.out.println(sb); } catch(IOException e) { // if I/O error occurs e.printStackTrace(); } finally { // releases any system resources associated with the stream if(sw!=null) sw.close(); if(bw!=null) bw.close(); } } }
Output
Let us compile and run the above program, this will produce the following result:
Hello World!
Example - Writing a Simple String to a File
The following example shows the usage of Java BufferedWriter write(String c) method.
BufferedWriterDemo.java
package com.tutorialspoint; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) { String filePath = "example.txt"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { // Write a string to the file writer.write("Hello, World!"); System.out.println("String written to the file."); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result −
String written to the file.
Output in File (example.txt)
Hello, World!
Explanation
A BufferedWriter is created with a FileWriter to write to a file (example.txt).
The write(String s) method writes the entire string "Hello, World!" to the buffer.
The try-with-resources block ensures that the writer is closed automatically after writing, flushing the buffer content to the file.
Example - Writing Multiple Strings with Line Breaks
The following example shows the usage of Java BufferedWriter write(int c) method.
BufferedWriterDemo.java
package com.tutorialspoint; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterDemo { public static void main(String[] args) { String filePath = "example.txt"; try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) { // Write multiple strings, each followed by a new line writer.write("Line 1: Java Programming"); writer.newLine(); // Write a line separator writer.write("Line 2: BufferedWriter Example"); writer.newLine(); // Write another line separator writer.write("Line 3: Writing Strings with newLine()"); System.out.println("Strings written to the file with line breaks."); } catch (IOException e) { System.err.println("An error occurred: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result −
Strings written to the file with line breaks.
Output in File (example.txt)
Line 1: Java Programming Line 2: BufferedWriter Example Line 3: Writing Strings with newLine()
Explanation
A BufferedWriter is used to write multiple strings to a file (example.txt).
-
The write(String s) method writes each string to the buffer.
The newLine() method adds a platform-independent line break after each string, ensuring proper formatting in the output file.
-
The strings are written sequentially, with each string appearing on a new line.
Key Points About write(String s)
Writes Entire Strings− The method writes all characters of the provided string to the buffer.
Efficient for Text Content− Ideal for writing lines of text or complete strings to an output file or stream.
No Automatic Line Breaks− To write on a new line, you need to explicitly use the newLine() method.
When to Use This write(String s)
When you want to write entire strings to a file or output stream.
When writing lines of text that can optionally be combined with newLine() for structured formatting.
These examples highlight the simplicity and flexibility of the write(String s) method for writing text content using a BufferedWriter.