0

I have a simple program that needs to write a few lines of string to a file using FileWriter and BufferedWriter. It already reads the (in this case) exact same data from the file, it stores it in runtime memory, but it outright refuses to write it and I have no idea why. Usually I'd try to debug it and see what's happening but I have no idea what I'm looking for.

Here is my code:

public void close () {
    FileWriter output = null;
    BufferedWriter fileOut = null;

    // I use the exact same method to read from file so this should work
    // this.prajituri :: ArrayList
    // toString is @Override

    try {
        output = new FileWriter(this.fileName);
        fileOut = new BufferedWriter(output);

        for (int i = 0; i < this.prajituri.size(); i++) {
            fileOut.write(prajituri.get(i).toString());
            System.out.println(prajituri.get(i).toString());
            fileOut.newLine();
        }
    } catch (IOException ioe) {
        System.out.println("Output error: " + ioe);
    } finally {
        if (fileOut != null)
            fileOut = null;
        if (output != null)
            output = null;
    }
}

Just in case, here is my toString:

@Override
public String toString () {
    return this.name + ";" + this.weight + ";" + this.price;
}

Every time I try to write to file, it gives me an empty file. Am I doing something wrong ?

I checked and this.prajituri is actually good (has all the data it should have) And I use the System.out.println(prajituri.get(i).toString()) to check what it Should write and that is OK also. Yet it writes nothing.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Kalec
  • 2,681
  • 9
  • 30
  • 49

4 Answers4

4

you have to close your BufferedWriter fileOut.close()

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • 1
    +1, although alternatively you could flush it and let the system close it for you, but this should solve the problem. – corsiKa Jan 07 '13 at 16:59
  • 2
    @corsiKa - the system won't close it for you, you will end up with a file handle leak if you don't call close explicitly. – Paolo Jan 07 '13 at 17:02
  • @Paolo I shouldn't make comments before my morning coffee. You're absolutely right. The only time it would close it for you is when the process ends, and that's not exactly a good practice to be in... – corsiKa Jan 07 '13 at 17:32
2

You need to flush the stream to write it to disk.

flush in java.io.FileWriter

You should close the stream properly by calling fileout.close() (which will internally call the flush anyway).

Community
  • 1
  • 1
Paolo
  • 22,188
  • 6
  • 42
  • 49
2

To write to disk immediately:

output.flush();

When done:

output.close();

close( ) includes flush( )

Mel Nicholson
  • 3,225
  • 14
  • 24
1

You have to flush/ close your stream. If you're using Java SE 7, the easiest way is to use the new Automatic Resource Management feature:

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Puce
  • 37,247
  • 13
  • 80
  • 152