Setting and Getting Permissions设置和取得许可
来源:互联网 发布:js大于等于怎么写 编辑:程序博客网 时间:2024/06/08 08:04
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
- Setting and Getting Permissions设置和取得许可
- Setting file and folder permissions
- java 封装 setting和getting
- AD Group Type and Permissions Setting in Cognos
- Getting, setting, and removing tabindex values with JavaScript
- El表达式以及Getting和Setting方法中大小写问题
- Getting or Setting Pixels
- WinSCP:but error occurred while setting the permissions and/or timestamp
- Setting NTFS Permissions with C#
- Setting NTFS Permissions with C#
- Setting NTFS Permissions with C#[
- Setting Up LAMP: Getting Linux, Apache, MySQL, and PHP Working Together
- AudioToolbox之AudioFile.h(四)Getting and Setting Audio File Properties
- Xamarin Getting Started翻译系列八--许可
- open() not setting file permissions correctly
- Principals,Securables and Permissions
- Security and Permissions
- .NET Permissions and others
- 把图片文件放到Oracle数据库中以及从Oracle数据库中转储出图片到文件系统
- http接口自动化测试框架实现
- html meta中得到viewport指令
- 安卓开发——textAppearance的属性设置
- Jquery封装之拖拽解决遇到的一些小bug
- Setting and Getting Permissions设置和取得许可
- Python图像处理库PIL的基本概念介绍
- 查看mysql连接数 sql语句执行时间
- 安卓开发——TextView属性设置
- 【翻译自HVR官方手册】HVR数据复制软件介绍与术语解释
- HDU 5622 KK's Chemical(dp + 计数、特殊图的染色多项式)
- 字符串模式匹配KMP算法
- c#_使用emgu3.0操作本地摄像头
- 面试笔试杂项积累-leetcode 201-205