Sunday, November 20, 2022

Java BufferedWriter Class With Examples

Java BufferedWriter class is used to write text to a character-output stream. This class is used as a wrapper around any Writer (FileWriter and OutputStreamWriter) whose write() operations may be costly. java.io.BufferedWriter makes the write operation more efficient by buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. Without buffering, each invocation of a print() method would cause characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.

Java BufferedWriter constructors

  • BufferedWriter(Writer out)- Wraps the passed Writer and creates a buffering character-input stream that uses a default-sized input buffer. Default buffer size for BufferedWriter is 8192 bytes i.e. 8 KB.

    For example-

     BufferedWriter out = new BufferedWriter(new FileWriter("resources/abc.txt"));
     
  • BufferedWriter(Writer out, int sz)- Wraps the passed Writer and creates a new buffered character-output stream that uses an output buffer of the given size.

Java BufferedWriter methods

Methods in BufferedWriter class are as given below-

  • flush()- Flushes the stream.
  • newLine()- Writes a line separator.
  • write(int c)- Writes a single character.
  • write(char[] cbuf, int off, int len)- Writes a portion of an array of characters. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.
  • write(String s, int off, int len)- Writes a portion of a String. Parameter off is the offset from which to start reading characters and parameter len is the number of characters to write.

Java BufferedWriter examples

For using BufferedWriter steps are as follows-

  1. Create an instance of BufferedWriter wrapped around another Writer.
  2. Using that instance to write to a file.
  3. Close the stream, you should close the resources in finally block or you can use try-with-resources to automatically manage resources.

1. Using write(int c) method of the Java BufferedWriter class to write single character to a file.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(65); //writes A
      bw.write(66); //writes B
      bw.write(67); //writes C
    }
  }
}

2. Using write(char[] cbuf, int off, int len) method of the Java BufferedWriter class to write portion of a character array.

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    char[] buffer = str.toCharArray();
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(buffer, 0, 7);// takes portion from index 0..6
    }
  }
}

3. Using write(String s, int off, int len) of the Java BufferedWriter class to write a portion of a String.

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(str, 0, 7);// takes substring from index 0..6
    }
  }
}

4. Using newLine() method of the Java BufferedWriter class.

public class FileWriteRW {
  public static void main(String[] args) throws IOException {
    String str = "This is a test String";
    try (BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\Temp\\write.txt"))){
      bw.write(str, 0, 7);// takes substring from index 0..6
      bw.newLine(); // new line
      bw.write(str, 7, str.length()-7);
      bw.flush(); // flushes the stream
    }
  }
}

That's all for this topic Java BufferedWriter Class With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Character Streams in Java IO
  2. Byte Streams in Java IO
  3. Write to a File in Java
  4. How to Append to a File in Java
  5. Reading File in Java Using Files.lines And Files.newBufferedReader

You may also like-

  1. How to Read File From The Last Line in Java
  2. Difference Between Thread And Process in Java
  3. LinkedHashSet in Java With Examples
  4. String Vs StringBuffer Vs StringBuilder in Java
  5. Constructor Overloading in Java
  6. Python Program to Display Fibonacci Series
  7. Spring MVC Exception Handling Example Using @ExceptionHandler And @ControllerAdvice
  8. Lazy Initialization in Spring Using lazy-init And @Lazy Annotation

No comments:

Post a Comment