简体   繁体   English

使用Java创建不可删除的文件并对其进行读写

[英]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. 我想从外部使文件不可删除,并希望使用Java从程序对该文件进行读/写操作。

S0, I created the undeletable file using java by using the following code : S0,我使用以下代码使用java创建了不可删除文件:

    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. 或另一种方式来做到这一点。

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. 尽管我不熟悉cacls及其确切功能,但简要浏览一下文档会发现您拒绝执行命令的用户访问文件,然后希望该用户访问该文件。 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. 我建议使用/ p开关为当前用户授予他们读写文件所需的权限,然后对代码中的文件进行所需的操作,然后使用cacls / d撤消当前用户的权限。用户。 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 谨记一下,请特别怀疑“:f”部分,我会再说一遍,我不熟悉cacls,但是我确实阅读了文档

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). 可以通过使用其他用户帐户删除文件来删除该文件(例如,管理员)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM