Why this code does not work?

Avenger625's Avatar, Join Date: Feb 2011
Go4Expert Member
  1. import java.io.*;
  2. class File1
  3. {
  4. public static void main(String A[])
  5. {
  6. try
  7. {
  8. BufferedWriter fout=new BufferedWriter(new FileWriter("d:\\Test.txt",true));
  9. fout.write('A');
  10. }
  11. catch(Exception e)
  12. {
  13. System.err.println(e);
  14. }
  15. }
  16. }


The file gets created but it's an empty(0 byte)file.
The character 'A' should be there. Why it's not there????
0
Avenger625's Avatar, Join Date: Feb 2011
Go4Expert Member
Why the indentation is lost when I clicked the "Submit" button?????
0
Abhishek Vaja's Avatar, Join Date: May 2011
Newbie Member
Quote:
Originally Posted by Avenger625 View Post
  1. import java.io.*;
  2. class File1
  3. {
  4. public static void main(String A[])
  5. {
  6. try
  7. {
  8. BufferedWriter fout=new BufferedWriter(new FileWriter("d:\\Test.txt",true));
  9. fout.write('A');
  10. }
  11. catch(Exception e)
  12. {
  13. System.err.println(e);
  14. }
  15. }
  16. }
The file gets created but it's an empty(0 byte)file.
The character 'A' should be there. Why it's not there????




data is not shown because u didn't close the file.

Write fout.close();
after the write opertaion is performed on a file..........
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Avenger625 View Post
Why the indentation is lost when I clicked the "Submit" button?????
See http://www.go4expert.com/misc.php?do=bbcode#code
0
Avenger625's Avatar, Join Date: Feb 2011
Go4Expert Member
Why dont i need to call close() on FileOutputStream object???
Why is it mandatory to close() on BufferedWriter object????

More precisely, the following code works fine but the code in my 1st post does not work correctly. Why.....??????????

Code:
import java.io.*;

class File1
{
    public static void main(String A[])
    {
        try
        {
            //BufferedWriter fout=new BufferedWriter(new FileWriter("d:\\Usrdtls.bsk",true));
            
            FileOutputStream fout= new FileOutputStream("d:\\Usrdtls.bsk");
            fout.write(65);
            
            //fout.close();

        }
        catch(Exception e)
        {
            System.err.println(e);
        }
    }
}
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
buffer---> in memory
file--->in disk
0
Avenger625's Avatar, Join Date: Feb 2011
Go4Expert Member
Right now I have got few questions. Let the 1st one be answered first and then accordingly I'll ask the other questions.

"Buffering characters is way to provide, the efficient reading or writing of characters, arrays, and lines." - Would you please elaborate on this "efficiency"..I mean how is it efficient???
Please, help me understand with a suitable example...!!!
0
Avenger625's Avatar, Join Date: Feb 2011
Go4Expert Member
Right now I have got few questions. Let the 1st one be answered first and then accordingly I'll ask the other questions.

"Buffering characters is way to provide, the efficient reading or writing of characters, arrays, and lines." - Would you please elaborate on this "efficiency"..I mean how is it efficient???
Please, help me understand with a suitable example...!!!
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
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.
from here-->http://download.oracle.com/javase/1....redWriter.html
0
Avenger625's Avatar, Join Date: Feb 2011
Go4Expert Member
I have already read that and also the documentation for BufferedReader. From there only, this question of mine cropped up....about inefficiency. I need little more clarifications on it.....

Quote:
"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. "
1. If I'm buffering it, then what exactly is happening when we call print() each time????

(Please, answer this one first and then I have other questions to ask also. I will ask accordingly).

Ref: http://download.oracle.com/javase/1,...eamReader.html
http://download.oracle.com/javase/1,...redReader.html