您的位置:首页 > 其它

Setting and Getting Permissions设置和取得许可

2016-02-10 10:15 316 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: