JAVA文件访问控制
来源:互联网 发布:炒股模拟软件 知乎 编辑:程序博客网 时间:2024/06/05 17:13
JAVA访问控制具备四种权限read、write、delete、execute,implies方法用来确定某个线程是否拥有这些操作的权限
public boolean implies(Permission p) {
if (!(p instanceof FilePermission))
return false;
FilePermission that = (FilePermission) p;
return ((this.mask & that.mask) == that.mask) && impliesIgnoreMask(that);
}
boolean impliesIgnoreMask(FilePermission that) {
if (this.directory) {
if (this.recursive) {
// make sure that.path is longer then path so
// something like /foo/- does not imply /foo
if (that.directory) {
return (that.cpath.length() >= this.cpath.length()) &&
that.cpath.startsWith(this.cpath);
} else {
return ((that.cpath.length() > this.cpath.length()) &&
that.cpath.startsWith(this.cpath));
}
} else {
if (that.directory) {
// if the permission passed in is a directory
// specification, make sure that a non-recursive
// permission (i.e., this object) can't imply a recursive
// permission.
if (that.recursive)
return false;
else
return (this.cpath.equals(that.cpath));
} else {
int last = that.cpath.lastIndexOf(File.separatorChar);
if (last == -1)
return false;
else {
// this.cpath.equals(that.cpath.substring(0, last+1));
// Use regionMatches to avoid creating new string
return (this.cpath.length() == (last + 1)) &&
this.cpath.regionMatches(0, that.cpath, 0, last+1);
}
}
}
} else {
return (this.cpath.equals(that.cpath));
}
}
例子如下
package com.test;
import java.io.File;
import java.io.FilePermission;
import java.security.Permission;
public class Test2{
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, CloneNotSupportedException {
char s = File.separatorChar;
Permission file = new FilePermission("C:"+s+"Windows"+s+"f", "read");
Permission file1 = new FilePermission("C:"+s+"Windows"+s+"*", "read");
Permission file2 = new FilePermission("C:"+s+"Windows"+s+"f", "write");
Permission file3 = new FilePermission("C:"+s+"Windows"+s+"*", "write");
Permission file4 = new FilePermission("C:"+s+"Windows"+s+"f", "delete");
Permission file5 = new FilePermission("C:"+s+"Windows"+s+"*", "delete");
Permission file6 = new FilePermission("C:"+s+"Windows"+s+"f", "execute");
Permission file7 = new FilePermission("C:"+s+"Windows"+s+"*", "execute");
boolean f = file.implies(file);
boolean f1 = file.implies(file1);
boolean f2 = file.implies(file2);
boolean f3 = file.implies(file3);
boolean f4 = file.implies(file4);
boolean f5 = file.implies(file5);
boolean f6 = file.implies(file6);
boolean f7 = file.implies(file7);
System.out.println("file:"+f);
System.out.println("file1:"+f1);
System.out.println("file2:"+f2);
System.out.println("file3:"+f3);
System.out.println("file4:"+f4);
System.out.println("file5:"+f5);
System.out.println("file6:"+f6);
System.out.println("file7:"+f7);
}
}
执行结果为
file:true
file1:false
file2:false
file3:false
file4:false
file5:false
file6:false
file7:false
- JAVA文件访问控制
- 文件访问控制权限
- Linux控制文件访问
- Java程序访问权限控制(policy文件)
- java 访问控制符
- java 访问控制符
- java访问控制
- Java的访问控制
- java 访问控制符
- Java访问控制关键字
- java访问权限控制
- java访问控制符
- java中的访问控制
- Java中的访问控制
- Java的访问控制
- java访问控制
- JAVA访问级别控制
- java 访问控制符
- 策略模式
- Android开发错误处理
- 实现AJAX的基本步骤
- 给在读研究生+未来要读研同学们的一封受益匪浅的信
- [leetcode] Combination Sum II
- JAVA文件访问控制
- SD/MMC/SDIO 概要
- Response.WriteFile 无法下载大文件解决方法
- Android SDK更新以及ADT更新出现问题的解决办法
- oracle rpad()函数
- 分析国内独立开发者的现状,他们是如何生存的
- 学习笔记:tuple中的右值
- VC6.0如何创建以及调用动态链接库具体实例
- 百度地图标注例子