Java BufferedWriter Example



BufferedWriter class writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

BufferedWriter class Example

Let's see the simple example of writing the data to a text file sample.txt using Java BufferedWriter.
This example uses the try-with-resources statement to close the resources automatically.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * The class demonstrate the usage of BufferedWriter class methods.
 * @author javaguides.net
 *
 */

public class BufferedWriterExample {
    public static void main(String[] args) {
        try (FileWriter writer = new FileWriter("D:\\sample.txt");
             BufferedWriter buffer = new BufferedWriter(writer);) {
             buffer.write("Welcome to JavaGuides.");
             System.out.println("Success");
        } catch (IOException e) {
             e.printStackTrace();
        }
    }
}

Reference

https://www.javaguides.net/2018/08/bufferedwriter-class-in-java.html

Comments