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
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
相关文章推荐
- 给牛牛上编程课——第三次课(字符串的存储和打印、转义字符)
- Android TV -5- Building TV Channels
- HTML+CSS笔记 CSS笔记集合
- struts2文件上传下载(含中文编码问题)
- 在WebAPI中自动创建Controller
- 2016-2-10 tomcat基础学习
- 《步步深入委托二》——委托的两种用法
- 练习4.1-5最大子数组线性算法及证明
- 一些面试题目
- How Uuencoding Works
- html5在线画板
- hihocoder之01背包
- 教程Roll-a-ball tutorialMoving the Player 解读
- HTML+CSS笔记 CSS中级 一些小技巧
- c#之剪刀石头布
- c#之剪刀石头布
- HDU 4169 UVALive 5741 Wealthy Family
- c#之剪刀石头布
- UI控件(UISlider)
- 1018. 锤子剪刀布