tags:

views:

109

answers:

5

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?

+5  A: 

try fw.flush() and fw.close()

Sanjay Manohar
+1: ...but `bw.close()` automatically calls `bw.flush()`, so you could get away with just calling close.
R. Bemrose
thanks so much! i cant believe i dint do that :P
jillika iyer
:) that always happens in a hurry
LGAP
those should be bw.flush() and bw.close(), calling the fw functions wont flush the bufferedWriter
josefx
+8  A: 

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.

Tore A.
thanks so much! i dont know i how missed that! super stupid! sorry!
jillika iyer
@jillika: I'm sure it has happend to every single Java programmer out there, and more than once.
Michael Borgwardt
+2  A: 

Make sure you call bw.close() before your method exits. If the buffer doesn't get flushed your file wont get written.

Lazarus
+1  A: 

Try closing the stream with sw.close() or the data may still be cached and not actually written to disk.

Samuel_xL
+3  A: 

You need to flush the buffer and you should close the file as well:

try {
            fw = new FileWriter("/tmp/testfile.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("this is test");
            bw.write("this is test");
            bw.write("this is test");
            bw.flush();
            fw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Also you should handle the IOException from the actual file writing separately from the file closing so you won't leave the file descriptor opened at the end:

    FileWriter fw = null;
    try {
        fw = new FileWriter("/tmp/testfile.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("this is test");
        bw.write("this is test");
        bw.write("this is test");
        bw.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fw != null) {
                fw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
fikovnik
+1, the only as far who mentions closing in `finally`, the right way. Note that `flush()` is still unnecessary as `close()` implicitly does that. But that doesn't hurt. Not closing in `finally` may hurt more in exceptional cases.
BalusC
Closing the `FileWriter` stream is redundant. This is done implicitly by the `BufferedWriter` `close` method. Keep that in mind if you ever plan on using more than one Writer on a given stream. (That would be a case where you should use `flush()` and not `close()` until the end.)
Tore A.