Modifying File while in use using Java -


I have this recurring Java JAR program function that attempts to modify a file every 60 seconds.

The problem is that if the user is viewing the file compared to the Java program, then the file will not be able to modify. I get the typical IOException.

Does anybody know that there is currently no way to modify a file in use in Java? Or does anyone know what would be the best way to solve this problem?

I was thinking about using the file, read (), convert () methods to check whether the file is in use or not. If the file is used then I am thinking of creating a backup copy of the data that could not be written. Then after 60 seconds, some arguments can add whether the backup file is empty or not. If the backup file is not empty, add its content to the main file. If empty, then just add new data to the main file. Of course, for the first time I will always do this, check that the file is in use.

Thank you for all your thoughts.

I'm thinking of using the canRead () file, canRrite () methods Was that the file is in use or not.

No idea - you will participate in the race conditions eg when your code has used those check methods, then the correct return value is received, but Then open the file for writing it before locking it by another application (possibly user).

Instead, try to get one on the file and use "backup file" when it fails.


Comments