|
-
March 1st, 2000, 09:56 AM
#1
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());}
}
}
-
March 1st, 2000, 07:24 PM
#2
Re: Java output to a file question
you may forget to close your stream
pw.close();
good luck
Alfred Wu
-
March 2nd, 2000, 02:21 AM
#3
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());
}
}
}
-
March 2nd, 2000, 10:42 AM
#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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|