Hein Minn Soe
Hein Minn Soe

Reputation: 57

Creating undeletable file and reading/writing on it using java

I would like to make file undeletable from outside and want to make read/write operation on that file from program using java.

S0, I created the undeletable file using java by using the following code :

    Process pcs = Runtime.getRuntime().exec(
    "cmd /c start cacls E:\\PJ\\testing2.txt /e /d %username%");

It was success.But, I can't do read and write on that file anymore. Somebody help me to do this! Or another way to do this.

Upvotes: 0

Views: 606

Answers (1)

Mark W
Mark W

Reputation: 2803

While I'm not familiar with cacls, and what exactly it does, a brief look at the documentation says that your denying access to the file for the user executing the command, then you want that same user to access the file. I would recommend using the /p switch to give the current user the rights they need to read and write to the file, then do what you need to do to the file in code, then use cacls /d to revoke the rights for the current user again. Try doing something like this:

 Process pcs = Runtime.getRuntime().exec(
 "cmd /c start cacls E:\\PJ\\testing2.txt /e /p %username% : f");

 //Logic that manipulates the file here

 Process pcs = Runtime.getRuntime().exec(
 "cmd /c start cacls E:\\PJ\\testing2.txt /e /d %username%");

Just as a note, be especially suspect of that " : f " part, I'll say it again, I am not familiar with cacls, but I did read the documentation

Edit - It also occurs to me that this logic will only revoke rights for the current user, and thus the file is NOT 'undeleteable'. One could delete this file by using a different user account to delete the file (eg Administrator).

Upvotes: 1

Related Questions