限制用户删除,重命名和修改使用Java.in Windows创建的文件

时间:2017-04-04 08:48:26

标签: java file-io file-permissions java-io

我正在Windows中创建一个文件,并希望限制用户删除,重命名和修改创建的文件。我使用以下代码来创建文件并给予只读权限。但是,用户可以修改创建的文件。

非常感谢。

 SELECT department_name AS 'Department Name', COUNT(*) AS 'No of Employees'
 FROM departments INNER JOIN employees ON 
 employees.department_id = departments.department_id 
 GROUP BY departments.department_id, department_name
 ORDER BY COUNT(*) DESC, department_name Asc;

1 个答案:

答案 0 :(得分:1)

我尝试了以下方法来限制用户删除,重命名和修改文件。请尝试相同并使其工作。提供权限后,我们必须撤消用户的权限

public static void Permissions(String path) throws IOException{
            File file1 = new File(path);
            System.out.println(file1.getPath());

              if (file1.createNewFile()){
                    System.out.println("File is created!");
                  }else{
                    System.out.println("File already exists.");
                  }
              //1. Using CACLS cmd
//            Runtime.getRuntime().exec("CACLS myfile.txt /E /G hitesh_golhani:R" + path);

                //2. Using file methods
//               boolean a =  file.setExecutable(true,true);
//               boolean b =   file.setReadable(true,true);
//               boolean c =    file.setWritable(true,true);
//               file.setReadOnly();

              //3. Using FilePermission
//            SecurityManager sm = new SecurityManager();
//            FilePermission fp1 = new FilePermission(path, "read");
//            PermissionCollection pc = fp1.newPermissionCollection();
//               pc.add(fp1);


              //4. Using POSIX java 7
//            Set perms = new HashSet();
//            perms.add(PosixFilePermission.OWNER_READ);
//            perms.add(PosixFilePermission.OWNER_WRITE);
//            Files.setPosixFilePermissions(file.toPath(), perms);

              /*  //5. Using Path
              Path file = Paths.get(path);
              AclFileAttributeView aclAttr = Files.getFileAttributeView(file, AclFileAttributeView.class);
              System.out.println("owner------"+aclAttr.getOwner());
              for(AclEntry aclEntry : aclAttr.getAcl()){
                  System.out.println("entry----"+aclEntry);
              }
              System.out.println();

              UserPrincipalLookupService upls = file.getFileSystem().getUserPrincipalLookupService();
              UserPrincipal user = upls.lookupPrincipalByName(System.getProperty("hitesh_golhani"));
              AclEntry.Builder builder = AclEntry.newBuilder();       
              builder.setPermissions( EnumSet.of(AclEntryPermission.READ_DATA, AclEntryPermission.EXECUTE, 
                      AclEntryPermission.READ_ACL, AclEntryPermission.READ_ATTRIBUTES, AclEntryPermission.READ_NAMED_ATTRS,
                      AclEntryPermission.WRITE_ACL, AclEntryPermission.DELETE
              ));
              builder.setPrincipal(user);
              builder.setType(AclEntryType.ALLOW);
              aclAttr.setAcl(Collections.singletonList(builder.build()));*/

          System.out.println("Exe = "+file1.canExecute());
          System.out.println("Read = "+file1.canRead());
          System.out.println("Wrt = "+file1.canWrite());          
    }