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();