1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Why this code does not work?

Discussion in 'Java' started by Avenger625, May 9, 2011.

  1. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    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????
     
  2. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    Why the indentation is lost when I clicked the "Submit" button?????
     
  3. Abhishek Vaja

    Abhishek Vaja New Member

    Joined:
    May 9, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0




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

    Write fout.close();
    after the write opertaion is performed on a file..........:happy:
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
  5. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    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);
            }
        }
    }
    
     
  6. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    buffer---> in memory
    file--->in disk
     
  7. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    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...!!!
     
  8. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    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...!!!
     
  9. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
  10. Avenger625

    Avenger625 New Member

    Joined:
    Feb 1, 2011
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    0
    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.....

    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,5.0/docs/api/java/io/InputStreamReader.html
    http://download.oracle.com/javase/1,5.0/docs/api/java/io/BufferedReader.html
     

Share This Page