0

I want to create a File using Java, that allows the program to modify the contents, however any external user should not be able to go to the path and manually delete or update that file. In other words I want to revoke privilege from users to be able to delete/update that file. For example:

path = "C:\\newfile.txt"
File file = new File(path);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write("hello world");

The above code allows the program to write hello world to the file. I want to make sure that no one can delete the file manually from "C:\". How do I do that?

  • 3
    http://stackoverflow.com/questions/664432/how-do-i-programmatically-change-file-permissions – Bruno Dec 30 '16 at 16:22
  • 1
    @BrunoDM's answer is what you're looking for, but the thing is that it won't stop someone with the right privileges from going in, taking over the file, and then deleting it. I don't believe there is really anyway to prevent that outside of correctly configuring rights on the system itself. – nihilon Dec 30 '16 at 16:27
  • 1
    Possible duplicate of [How can I lock a file using java (if possible)](http://stackoverflow.com/questions/128038/how-can-i-lock-a-file-using-java-if-possible) – 001 Dec 30 '16 at 16:30
  • This is impossible. File permissions are based on who is doing the action, not how the action is being performed. The only way this can really work is if you create a service that runs as a superuser (and even then, anyone with root entitlements can still get at it). – Joe C Dec 30 '16 at 21:22

0 Answers0