Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Why this code does not work? (http://www.go4expert.com/forums/code-t25740/)

Avenger625 9May2011 21:50

Why this code does not work?
 
  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????

Avenger625 9May2011 21:52

Re: Why this code does not work?
 
Why the indentation is lost when I clicked the "Submit" button?????

Abhishek Vaja 10May2011 01:36

Re: Why this code does not work?
 
Quote:

Originally Posted by Avenger625 (Post 82825)
  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..........:happy:

shabbir 10May2011 09:27

Re: Why this code does not work?
 
Quote:

Originally Posted by Avenger625 (Post 82826)
Why the indentation is lost when I clicked the "Submit" button?????

See http://www.go4expert.com/misc.php?do=bbcode#code

Avenger625 14May2011 02:00

Re: Why this code does not work?
 
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);
        }
    }
}


virxen 15May2011 01:14

Re: Why this code does not work?
 
buffer---> in memory
file--->in disk

Avenger625 15May2011 22:10

Re: Why this code does not work?
 
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...!!!

Avenger625 15May2011 22:18

Re: Why this code does not work?
 
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...!!!

virxen 17May2011 02:54

Re: Why this code does not work?
 
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

Avenger625 17May2011 10:37

Re: Why this code does not work?
 
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


All times are GMT +5.5. The time now is 09:18.