Click to See Complete Forum and Search --> : Java output to a file question


xxdxxd
March 1st, 2000, 08:56 AM
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());}

}

}

kib63613
March 1st, 2000, 06:24 PM
you may forget to close your stream
pw.close();
good luck
Alfred Wu

March 2nd, 2000, 01:21 AM
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());
}

}

}

keithmcelhinnney
March 2nd, 2000, 09:42 AM
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