1

I have an java app that is trying to write to a csv with some 30+ fields and many rows. If the app is killed while writing a line in csv, i sometimes see a half written line. Is there a way in java that can write a line atomically?

My requirement is that either the line should be written completely, not missing some fields due to abrupt termination.

Code that I use:

            File logFile = new File(csvLogPath, logFileName);
            String fileContent = "a,b,c,d,e,f,g";  //around 500b to 1.5kb usually

            FileWriter datawriter = new FileWriter(logFile, true);
            datawriter.append(fileContent);
            datawriter.append("\n");
            datawriter.flush();
            datawriter.close();
Sathish Kumar
  • 313
  • 2
  • 15
  • Post the code that you use to write to the file. – Andrew Henle Apr 19 '18 at 12:59
  • My guess is that you're forgetting to `flush` the data when writing. But it's impossible to see without the code. https://stackoverflow.com/questions/2340106/what-is-the-purpose-of-flush-in-java-streams – Sam Apr 19 '18 at 13:00
  • In short: No. You can lower the risk by using a `BufferedWriter` or `BufferedOutputStream` and call `flush()` after passing a completed line in, but you cannot completely avoid it. Java cannot look into future... – Timothy Truckle Apr 19 '18 at 13:00
  • Is there a way based on size (<2kb) the disk write is atomic, even at any point the java process can be killed? – Sathish Kumar Apr 19 '18 at 13:07
  • You could try doing the writing in a background thread. Then if it's the GUI that's closed (not killed) the background thread will finish. I'm not sure if you're talking about an android app - I'm not an android developer. But that would work in JavaFX. – Sam Apr 19 '18 at 13:10
  • What OS? Depending on your OS, even low-level writes to the file might not be guaranteed to be atomic. – Andrew Henle Apr 19 '18 at 13:12
  • The best you can try doing is to write to a temporary file and then rename it with the proper name with `java.nio.Files.move(..., StandardCopyOption.ATOMIC_MOVE)`. This way you either end up with a new file or with no file at all. – yegodm Apr 19 '18 at 13:13
  • Sorry if I wasn't very clear, this is not an android app, this is kind of backend java based tool that is running on linux. Collects data and writes it to CSV file. sometimes there are other ops tools that kills this based on circumstances, I see the broken csv line (rarely though). Just want to see if there is a specific Java file writer class that can write it atomically associated with a specific buffer size there by achieving an atomic write effect. – Sathish Kumar Apr 19 '18 at 13:14

0 Answers0