限制用户删除、重命名和修改在Windows中使用Java.com创建的文件
我正在Windows中创建文件,希望限制用户删除、重命名和修改创建的文件。我正在使用下面的代码创建该文件,并已授予只读权限。但是,用户可以修改创建的文件 非常感谢限制用户删除、重命名和修改在Windows中使用Java.com创建的文件,java,file-io,file-permissions,java-io,Java,File Io,File Permissions,Java Io,我正在Windows中创建文件,希望限制用户删除、重命名和修改创建的文件。我正在使用下面的代码创建该文件,并已授予只读权限。但是,用户可以修改创建的文件 非常感谢 public class checkFilePermission { private static String path = "C:\\Users\\abc_user\\Desktop\\VI\\test123.txt"; public static void main(String[] args) {
public class checkFilePermission {
private static String path = "C:\\Users\\abc_user\\Desktop\\VI\\test123.txt";
public static void main(String[] args) {
checkPermissions(path);
}
public static void checkPermissions(String path){
File file = new File(path);
System.out.println(file.getPath());
if(file.canExecute()){
System.out.println("Executable file");
}
if(file.canRead()){
System.out.println("Readable file");
}
if(file.canWrite()){
System.out.println("Writeable file");
}
//We can set the permissions as well.
file.setExecutable(false);
file.setReadable(false);
file.setWritable(false);
//file.setReadOnly();
System.out.println("Exe = "+file.canExecute());
System.out.println("Read = "+file.canRead());
System.out.println("Wrt = "+file.canWrite());
}
}
我尝试了以下方法来限制用户删除、重命名和修改文件。请尝试同样的方法并使其起作用。在提供权限之后,我们必须从用户那里撤销权限
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());
}
file.setExecutable
等。返回布尔值以指示它们是否成功。值得检查一下。@Michael,Code初学者需要一个逻辑来限制用户删除、重命名或修改创建的文件。上面的代码只是做了一些试验。在设置之前和之后检查权限。Thanks@HiteshKumar我完全明白。诊断过程的第一步是检查这些函数的返回值。它们可能返回false,但如果不是呢?我检查:'boolean a=file.setExecutable(false);布尔b=file.setReadable(false);布尔值c=file.setWritable(false);//file.setReadOnly();系统输出打印LN(a+“-”+b+“-”+c);'结果:false false True我正在尝试icacls命令授予/拒绝权限,并使用java运行该命令。删除访问:icacls路径/Remove:g用户名:(OI)(CI)F/T
提供读取访问:icacls路径/grant用户名:(OI)(CI)R/T