Search code examples
javaandroideclipsefilewriter

FileWriter won't write all of the file - yes, I have called close()


Here is the code:

        FileWriter writer = new FileWriter(csv);
        for (String line: lines) {
            writer.write(line);
            writer.write("\r\n");
            Log.d(TAG, line);
        }
        writer.close();

Each line gets shown perfectly in LogCat and no errors are thrown anywhere, but the actual file cuts off at the end of the second line.

Edit:

Turns out the problem was a known bug with Android: https://code.google.com/p/android/issues/detail?id=38282

I fixed the problem by adding the following line after writer.close:

MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, null, null);


Solution

  • Turns out the problem was a known bug with Android: https://code.google.com/p/android/issues/detail?id=38282

    I fixed the problem by adding the following line after writer.close:

    MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, null, null);