I use FileOutputStream fos to create/append and write to a debug file in a separate thread:
fos = new FileOutputStream(outputFile, true);
if (!outputFile.exists()) {
outputFile.createNewFile();
}
fos.write(string.getBytes());
This code keeps beeing called in a loop while the user might clear=delete the file anytime from another thread to start logging the debug output anew.
Now I am really surprised that when the file is beeing deleted this code runs through without raising any exceptions BUT nothing happens. ie the file is not beeing recreated nor is anything written anmore. All the “writes” are just lost.
Do I really have to check manually all the time whether the file has been deleted in the meanwhile myself before calling fos.write each time?
If the exception would work like expected, ie when writing to a deleted file, it would be just easier and cleaner.
This is totally OS dependent. On Windows, you wouldn’t be able to delete the file; but on UNIX-like systems, this is just how it works, independent of programming language. Given that a file can have multiple links which can be deleted independently, you should understand why this behavior makes sense. And it fact, it leads to easier and cleaner behavior than exploding when a file is deleted would.