Home  >  Core Java

java.io.BufferedWriter Example in Java

By Arvind Rai, September 27, 2013
java.io.BufferedWriter writes text to character output stream. BufferedWriter buffers the writing text so that it can write efficiently to a file or any other output stream source. The buffered size can be limit by specifying the size. BufferedWriter gives greater performance in writing text. In my example I have taken a string and using BufferedWriter, I have written it to a file.

BufferedWriterTest.java
package com.concretepage.io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class BufferedWriterTest {
    public static void main(String[] args) {
        File file = new File("C:\\test.txt");
        String str = "Hello World! \nThis is BufferedReader Program.";
        BufferedWriter bw = null;
        try {
          Writer w = new OutputStreamWriter(new FileOutputStream(file));  
          bw = new BufferedWriter(w);
          bw.write(str);
        } catch (IOException e) {
           e.printStackTrace();
        }finally {
            try {
                bw.close();
              } catch (IOException ex) {
                ex.printStackTrace();
              }
         }
        System.out.println("Done");
    }
}
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us