osiegmar / FastCSV

CSV library for Java that is fast, RFC-compliant and dependency-free.
https://fastcsv.org/
MIT License
542 stars 93 forks source link

Can not write data to a file via CsvWriter on Android 33 #81

Closed Gelassen closed 1 year ago

Gelassen commented 1 year ago

Describe the bug Can not write data to a file via CsvWriter despite on a pure FileWriter has wrote this data.

To Reproduce

  1. git clone https://github.com/Gelassen/words-in-memory.git
  2. git checkout bugfix/reproduce-csvwriter-issue
  3. Run the app on device/emulator with Android 33
    Please check https://github.com/Gelassen/words-in-memory with branch bugfix/reproduce-csvwriter-issue

Additional context Happens across Android 33, Android 30. On Android 26 this code has an issue with overall access to destination folder, I am exploring it right now.

osiegmar commented 1 year ago

Please don't create bug tickets without a proper JUnit test.

Your problem is most likely not a bug but caused by a missing invocation of the close() method of the CsvWriter instance (after line: https://github.com/Gelassen/words-in-memory/blob/fdc26674fbbc9eac9a97e625cb9b71a67d276583/app/src/main/java/io/github/gelassen/wordinmemory/backgroundjobs/BackupVocabularyWorker.kt#L60). Therefore the output buffer is never written to disk.

Proper use (as shown in README):

try (CsvWriter csv = CsvWriter.builder().build(path)) {
    csv
        .writeRow("header1", "header2")
        .writeRow("value1", "value2");
}

...or in Kotlin:

CsvWriter.builder().build(path).use { csv ->
    csv
        .writeRow("header1", "header2")
        .writeRow("value1", "value2");
}
Gelassen commented 1 year ago

flush() call is what actually write data from a buffer to a destination endpoint. The flush() is called on each writeRow() call, close() call might (but is not obligated to by the contract!) to call flush(), but it just close stream and release resources. Correct me if you see the issue here.

Anyway I have tried to launch code with close and the issue is still here. I shared a minimal sample to reproduce the issue, a relevant unit test will take extra time. I will open a new issue when it would be prepared.

Gelassen commented 1 year ago

Update an hour later:

You was right - close() call fixed the issue. By some reason I missed updated data on the sdcard. Also might understanding of flush() and close() calls shared above is not fully correct, I need to dive deeper into this topic.

Thank you for your time and my apologies for bothering you with this.