Let's see step by step how to make use of the java.io.BufferedWriter class to write to a file in Java.
- First let's create a String that holds data that we need to write to a file.
String data = "Some data to write to a file";
- Next, let's say we want to write to a file named data.txt
String fileName = "data.txt";
- Next we need to create an object of java.io.FileWriter and pass in the fileName and a boolean flag to say if we want text to be appended to the file or not.
FileWriter fileWriter = new FileWriter(fileName, true);
- Now we can make use of the object of java.io.BufferedWriter and pass the FileWriter object to its constructor. BufferedWriter will create a buffered character-output stream which is a file in our case.
- Next, make use of the write() method from BufferedWriter to write the String data to the file.
- Finally make sure to close the stream using the close() method.
Complete Code:
package org.code2care.example;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* <pre>
* Example: Write a file using BufferedWriter
*
* Author: Code2care.org
* Version: v1.0
* Date: 5th July 2023
* </pre>
*/
public class WriteFileUsingBufferedWriterExample {
public static void main(String[] args) {
String data = "Some data to write to a file";
String fileName = "data.txt";
try (FileWriter fileWriter = new FileWriter(fileName, true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
bufferedWriter.write(data);
} catch (IOException ioException) {
System.out.println("Error occurred writing the file!");
}
}
}
Output:

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to Change Mouse Wheel Scroll Direction on Mac - MacOS
- Add comma or semicolon at end of each line Notepad++ - NotepadPlusPlus
- Upgrade Gradle in Android Studio - Gradle
- Tailwind CSS Hello World Example - CSS
- How to stop MongoDB Server running on Ubuntu - Ubuntu
- [Fix] java: incompatible types: java.lang.String cannot be converted to int - Java
- Get count of SharePoint List or Document Library Items using PowerShell - SharePoint
- Fix: Eclipse Cant Connect to any repository not Authorized Error GitHub - Eclipse