简体   繁体   English

Java创建不可删除文件

[英]Java Create Undeletable File

Is there any method to create a file in java that cannot be deleted. 是否有任何方法可以在Java中创建无法删除的文件。 I have googled it and found processes involving the cmd. 我已经用谷歌搜索,发现涉及cmd的进程。 However, I require a pure "java" way that can be done on any platform. 但是,我需要可以在任何平台上完成的纯“ java”方式。

Thanks in advance. 提前致谢。

Thank you for your help. 谢谢您的帮助。 I finally got it right. 我终于明白了。 I used the following code to deny access to user 我使用以下代码拒绝访问用户

public static void main() throws IOException
{
    Path file = Paths.get("c:/b.txt");
    AclFileAttributeView aclAttr = Files.getFileAttributeView(file, AclFileAttributeView.class);
    //System.out.println();

    UserPrincipalLookupService upls = file.getFileSystem().getUserPrincipalLookupService();
    UserPrincipal user = upls.lookupPrincipalByName(System.getProperty("user.name"));
    AclEntry.Builder builder = AclEntry.newBuilder();       
    builder.setPermissions(EnumSet.of(AclEntryPermission.APPEND_DATA, AclEntryPermission.DELETE, AclEntryPermission.DELETE_CHILD, AclEntryPermission.EXECUTE, AclEntryPermission.READ_ACL, AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.READ_DATA, AclEntryPermission.READ_NAMED_ATTRS, AclEntryPermission.SYNCHRONIZE, AclEntryPermission.WRITE_ACL, AclEntryPermission.WRITE_ATTRIBUTES, AclEntryPermission.WRITE_DATA, AclEntryPermission.WRITE_NAMED_ATTRS, AclEntryPermission.WRITE_OWNER));
    builder.setPrincipal(user);
    builder.setType(AclEntryType.DENY);
    aclAttr.setAcl(Collections.singletonList(builder.build()));
}

Try the method setPosixFilePermissions() and set the permissions to read only for all the classes of users. 尝试使用方法setPosixFilePermissions(),并将所有用户类别的权限设置为只读。 Refer this - http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#setPosixFilePermissions%28java.nio.file.Path,%20java.util.Set%29 请参考-http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#setPosixFilePermissions%28java.nio.file.Path,%20java.util.Set%29

If you want to create a file that can't be accidentally overwritten, then look at the various answers to this: How do i programmatically change file permissions? 如果要创建一个不会被意外覆盖的文件,请查看以下各种答案: 如何编程方式更改文件权限?

If you want to create a file that the current program cannot delete at all (but a privileged one could), it might be possible by setting permissions appropriately on the parent directory, or possibly using SELinux Mandatory Access Control cleverness. 如果要创建一个当前程序根本无法删除的文件(但是有特权的文件可以删除),则可以通过在父目录上适当设置权限或使用SELinux强制访问控制的技巧来实现。

If you want to create a truly undeleteable file, then you are out of luck. 如果您要创建一个真正不可删除的文件,那么您很不走运。 I am not aware of any operating system that supports creation of files that can never be deleted. 我不知道任何支持创建永不删除的文件的操作系统。 It would be an "anti-feature". 这将是“反特征”。


I would also agree with @Teifi's comment. 我也同意@Teifi的评论。 Create a file that cannot ever be deleted on the user's machine is not acceptable ... unless done by, or with the authorization of the system's administrators. 创建无法在用户计算机上删除的文件是不可接受的...除非由系统管理员或在系统管理员的授权下完成。 I would call any software that did that "malicious" too. 我也称做该软件的任何软件都是“恶意的”。

You can lock the file using java.nio.* . 您可以使用java.nio.*锁定文件。 Here is a tutorial how to use it http://www.javabeat.net/2007/10/locking-files-using-java/ . 这是http://www.javabeat.net/2007/10/locking-files-using-java/的使用方法。

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

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