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());}
}
}
Re: Java output to a file question
you may forget to close your stream
pw.close();
good luck
Alfred Wu
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());
}
}
}
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