java 判断目录权限,如何在java中检查目录的写权限?

I would like a code snippet that checks whether a directory has read/write permissions and do something if it does, and does something else if it doesnt. I tried an example shown here:

try {

AccessController.checkPermission(new FilePermission("/tmp/*", "read,write"));

System.out.println("Good");

// Has permission

} catch (SecurityException e) {

// Does not have permission

System.out.println("Bad");

}

The problem is the exception is always triggered, so it always ends up printing "Bad" regardless of whether the directory has write permissions or not. (I chmod the directories to 777 or 000 to test).

Is there an alternative or some way to accomplish what I need?

解决方案

if you just want to check if you can write:

File f = new File("path");

if(f.canWrite()) {

// write access

} else {

// no write access

}

for checking read access, there is a function canRead()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值