1

Is there any method to create a file in java that cannot be deleted. I have googled it and found processes involving the cmd. However, I require a pure "java" way that can be done on any platform.

Thanks in advance.

Pratanu Mandal
  • 597
  • 8
  • 23
  • 7
    can you explain what you mean by "undeletable"? With the proper rights, most OSes allow you to delete any files... – assylias Mar 20 '13 at 13:34
  • Seeing as you are talking about cmd, do you mean: http://fixitwizkid.com/threads/make-an-undeletable-file.1445/ As an undeletable file? – Floris Velleman Mar 20 '13 at 13:37
  • 1
    Is this any help? http://stackoverflow.com/questions/664432/how-do-i-programmatically-change-file-permissions – Andrew Fielden Mar 20 '13 at 13:37
  • Furthermore, things like this are different for each platform, so even if you use Java it will not be portable (or you'll have to do something like (pseudo code) `if (OS.isWindows) { /* windows way */ } else if (OS.isMac) { /* Mac way */ } etc...` – 11684 Mar 20 '13 at 13:39
  • 1
    Stopping a user from deleting a file is what I would call malicious software. – Mike Baxter Mar 20 '13 at 13:45

3 Answers3

2

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()));
}
Pratanu Mandal
  • 597
  • 8
  • 23
  • can you give me ACLFileAttributeView and UserPrincipalLookUpService. bcz it cant resolve.. thank you for your code.. i want to implement it.. but i cant.. – Sagar Chavada Jul 08 '16 at 07:14
0

Try the method setPosixFilePermissions() and set the permissions to read only for all the classes of users. Refer this - http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#setPosixFilePermissions%28java.nio.file.Path,%20java.util.Set%29

Rakesh K
  • 8,237
  • 18
  • 51
  • 64
0

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.

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. 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.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • You don't understand my perspective. I do not intend to use it for malicious purposes. I will store a hashed password in the file so attackers should not be able to modify or delete the file. Although I had posted an answer, it does not help me because the file can still be overwritten so it's pretty useless against hackers. – Pratanu Mandal Oct 18 '14 at 07:43
  • I understand your perspective. You are trying to "do good". But you clearly do not understand mine ... which is that ultimate control of the machine does not rightly belong to you, *irrespective of your motives*. – Stephen C Oct 18 '14 at 09:25
  • OK ... I respect your perspective. Then, will you please help me out? Is there any folder (or "safe" registry key) in Windows where I can store a hashed password and be certain that hackers cannot modify or delete it? I really don't need to make a file undeletable, rather I just want a safe location for password storage. Please, do not suggest a database; online database is not an option and offline database can be easily deleted. – Pratanu Mandal Oct 18 '14 at 11:48
  • please help me out, will you? – Pratanu Mandal Oct 19 '14 at 06:29
  • *"If you want to create a truly undeleteable file, then you are out of luck."* - Did you read that? – Stephen C Oct 19 '14 at 06:31
  • Yes, I know that. However, Avast! Antivirus creates a file which is quite difficult to delete. Isn't it possible to create something like that? I tried modifying ACL Permissions but it does not help. – Pratanu Mandal Oct 19 '14 at 07:23