I am using this code to write to a file in java. it has always worked and I am 100% sure its right. But still the file does not get written. I don't even get an error.
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class writetofile {
public static void main(String [] args){
FileWriter fw;
try {
fw = new FileWriter("testfile.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("this is test");
bw.write("this is test");
bw.write("this is test");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Could it be some other problem?
You are not calling the close() method on the BufferedWriter object. That means the buffers never get flushed. Add bw.close() after your last bw.write() statements.
try fw.flush() and fw.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With