-2

I am unable to get the output while using BufferedWriter. From my point of view it will show

d

aei

durga

[where i posted why no output particularly]

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;

public class BufferedWriterDemo
{
    public static void main(String s[])throws Exception
    {
        //char b='a';
        File f=new File("abc.txt");
        //f.createNewFile();
        FileWriter fw=new FileWriter(f);
        FileReader fr=new FileReader(f);
        BufferedWriter bw=new BufferedWriter(fw);
        bw.write(100);
        bw.newLine();
        char ch1[]={'a','e','i'};
        bw.write(ch1);
        bw.newLine();
        bw.write("durga");
        BufferedReader br=new BufferedReader(fr);
        String line=br.readLine();
        while(line!=null)
        {
            System.out.println(line);
            line=br.readLine();
        }
        bw.flush();
        br.close();
        bw.close();
    }
}

Output Shown

//i didnt got anything

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Because a BufferedWriter doesn't actually write anything until you write 8 kilobytes, or flush it, or close it? (That's the *entire point* of BufferedWriter) – user253751 Mar 16 '15 at 09:16
  • As an aside, your variable names are *very* confusing. You've used `br` for a Buffered**Writer** and `bw` for a Buffered**Reader** and the same for file readers/writers. I would expect them to be the other way round, with a `w` suffix for writer and an `r` suffix for reader... – Jon Skeet Mar 16 '15 at 09:18
  • @immibas I didn't understood can u explain in detail a little bit Do i need to add more data into the file ? – mansi kbchawla Mar 16 '15 at 09:24

1 Answers1

1

After:

bw.write("durga");
  • Try doing:

    bw.flush();
    

    so it could write the data into the file, or

  • close the file handle there as you are done writing to the file.

As the name suggest, it buffers the data: once buffer is full, it pushes through to file in your example. But you are just writing few data, so i would suggest either close or flush your stream.

user207421
  • 305,947
  • 44
  • 307
  • 483
SMA
  • 36,381
  • 8
  • 49
  • 73