Setting and Getting Permissions设置和取得许可

Setting and Getting Permissions
Java 1.2 added a boolean setReadOnly() method to the File class to mark a file or directory as
read-only. However, a method to revert the file or directory to the writable state wasn’t added. More
importantly, until Java 6’s arrival, File offered no way to manage an abstract pathname’s read, write,

and execute permissions.

Java 6 added to File boolean setExecutable(boolean executable), boolean
setExecutable(boolean executable, boolean ownerOnly), boolean setReadable(boolean
readable), boolean setReadable(boolean readable, boolean ownerOnly), boolean
setWritable(boolean writable), and boolean setWritable(boolean writable, boolean ownerOnly)
methods that let you set the owner’s or everybody’s execute, read, and write permissions for the file
identified by the File object’s abstract pathname. Android also supports these methods:
 boolean setExecutable(boolean executable, boolean ownerOnly) enables
(pass true to executable) or disables (pass false to executable) this abstract
pathname’s execute permission for its owner (pass true to ownerOnly) or
everyone (pass false to ownerOnly). When the filesystem doesn’t differentiate
between the owner and everyone, this permission always applies to everyone. It
returns true when the operation succeeds. It returns false when the user doesn’t
have permission to change this abstract pathname’s access permissions or
when executable is false and the filesystem doesn’t implement an execute
permission.
 boolean setExecutable(boolean executable) is a convenience method that
invokes the previous method to set the execute permission for the owner.
 boolean setReadable(boolean readable, boolean ownerOnly) enables (pass
true to readable) or disables (pass false to readable) this abstract pathname’s
read permission for its owner (pass true to ownerOnly) or everyone (pass false
to ownerOnly). When the filesystem doesn’t differentiate between the owner and
everyone, this permission always applies to everyone. It returns true when the
operation succeeds. It returns false when the user doesn’t have permission to
change this abstract pathname’s access permissions or when readable is false
and the filesystem doesn’t implement a read permission.
 boolean setReadable(boolean readable) is a convenience method that invokes
the previous method to set the read permission for the owner.
 boolean setWritable(boolean writable, boolean ownerOnly) enables (pass
true to writable) or disables (pass false to writable) this abstract pathname’s
write permission for its owner (pass true to ownerOnly) or everyone (pass false
to ownerOnly). When the filesystem doesn’t differentiate between the owner and
everyone, this permission always applies to everyone. It returns true when the
operation succeeds. It returns false when the user doesn’t have permission to
change this abstract pathname’s access permissions.
 boolean setWritable(boolean writable) is a convenience method that invokes
the previous method to set the write permission for the owner.
Along with these methods, Java 6 retrofitted File’s boolean canRead() and boolean canWrite()
methods, and introduced a boolean canExecute() method to return an abstract pathname’s access
permissions. These methods return true when the file or directory object identified by the abstract
pathname exists and when the appropriate permission is in effect. For example, canWrite() returns
true when the abstract pathname exists and when the application has permission to write to the file.
The canRead(), canWrite(), and canExecute() methods can be used to implement a simple utility
that identifies which permissions have been assigned to an arbitrary file or directory. This utility’s
source code is presented in Listing 11-7.

Listing 11-7. Checking a File’s or Directory’s Permissions
import java.io.File;
public class Permissions
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("usage: java Permissions filespec");
return;
}
File file = new File(args[0]);
System.out.println("Checking permissions for " + args[0]);
System.out.println(" Execute = " + file.canExecute());
System.out.println(" Read = " + file.canRead());
System.out.println(" Write = " + file.canWrite());
}
}
Compile Listing 11-7 (javac Permissions.java). Assuming a readable and executable (only) file
named x in the current directory, java Permissions x generates the following output:
Checking permissions for x
Execute = true
Read = true
Write = false

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值