CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2000
    Posts
    2

    Java output to a file question

    I want to output some strings to a file, but the result file has nothing in it, what is wrong with the following code? Thanks.
    --------------------------------

    import java.io.*;

    public class testfile {

    public static void main(String[] args) {

    try{
    File outFile=new File("out.txt");
    FileWriter out=new FileWriter(outFile);
    BufferedWriter pw=new BufferedWriter(out);
    pw.write ("aaaaa");
    pw.write("bbbbbb");
    pw.write("ccccc");

    }catch(IOException e){
    System.err.println(e.toString());}

    }

    }




  2. #2
    Join Date
    Dec 1999
    Location
    Chonghe, Taipei County, Taiwan, R.O.C.
    Posts
    231

    Re: Java output to a file question

    you may forget to close your stream
    pw.close();
    good luck
    Alfred Wu


  3. #3
    Guest

    Re: Java output to a file question

    import java.io.*;

    public class testfile {

    public static void main(String[] args) {

    try{
    //File outFile=new File("out.txt");
    //FileWriter out=new FileWriter(outFile);
    FileWriter out=new FileWriter("out.txt");
    BufferedWriter pw=new BufferedWriter(out);
    pw.write ("aaaaa");
    pw.write("bbbbbb");
    pw.write("ccccc");

    /*********************you should flush() BufferedWriter objects after writing /****them
    pw.flush();
    pw.close();
    out.close();
    /*********************
    }catch(IOException e){
    System.err.println(e.toString());
    }

    }

    }



  4. #4

    Re: Java output to a file question

    Try closing the file with pw.close(). If you are on win 9x you might be having a problem with the buffered writer. Set the buffer size to 1 and try again.

    keith

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured