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.