Why this code does not work?

Go4Expert Member
9May2011,21:50   #1
Avenger625's Avatar
  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????
Go4Expert Member
9May2011,21:52   #2
Avenger625's Avatar
Why the indentation is lost when I clicked the "Submit" button?????
Newbie Member
10May2011,01:36   #3
Abhishek Vaja's Avatar
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..........
Go4Expert Founder
10May2011,09:27   #4
shabbir's Avatar
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
Go4Expert Member
14May2011,02:00   #5
Avenger625's Avatar
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);
        }
    }
}
Pro contributor
15May2011,01:14   #6
virxen's Avatar
buffer---> in memory
file--->in disk
Go4Expert Member
15May2011,22:10   #7
Avenger625's Avatar
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...!!!
Go4Expert Member
15May2011,22:18   #8
Avenger625's Avatar
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...!!!
Pro contributor
17May2011,02:54   #9
virxen's Avatar
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
Go4Expert Member
17May2011,10:37   #10
Avenger625's Avatar
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