Java中的文件权限详细介绍

2021年3月10日16:12:00 发表评论 707 次浏览

Java提供了许多方法调用来检查和更改文件的权限, 例如可以将只读文件更改为具有写权限。当用户想要限制文件上允许的操作时, 需要更改文件许可权。例如, 由于用户不再希望编辑文件, 因此可以将文件许可权从写更改为只读。

检查当前文件权限

文件可以是以下允许的权限的任意组合:

可执行文件:

测试应用程序是否可以执行此抽象路径名表示的文件。

语法如下:

public boolean canExecute()
Returns: true if and only if the abstract path name
exists and the application is allowed to execute the file

可读性:

测试应用程序是否可以读取此抽象路径名表示的文件。

语法如下:

public boolean canRead()
Returns: true if and only if the file specified by this
abstract path name exists and can be read by the application; false otherwise

可写的:

测试应用程序是否可以修改此抽象路径名表示的文件。

语法如下:

public boolean canWrite()
Returns: true if and only if the file system actually 
contains a file denoted by this abstract path name and 
the application is allowed to write to the file; false otherwise.

例如, 文件可以读取和写入, 但不能执行。这是一个Java程序, 用于获取与文件关联的当前权限。

// Java program to check the current file permissions.
import java.io.*;
  
public class Test
{
     public static void main(String[] args)
     {
         // creating a file instance
         File file = new File( "C:\\Users\\Mayank\\Desktop\\1.txt" );
          
         // check if the file exists
         boolean exists = file.exists();
         if (exists == true )
         {
             // printing the permissions associated with the file
             System.out.println( "Executable: " + file.canExecute());
             System.out.println( "Readable: " + file.canRead());
             System.out.println( "Writable: " + file.canWrite());
         }
         else
         {
             System.out.println( "File not found." );
         }
     }
}

输出如下

Executable: true
Readable: true
Writable: true

更改文件权限

文件可以具有以下权限的任意组合:

  • 可执行的
  • 可读的
  • 可写的

以下是更改与文件关联的权限的方法:

setExecutable

一种方便的方法, 用于为此抽象路径名设置所有者的执行权限。

public boolean setExecutable(boolean executable)
Description: 
Parameters: executable - If true, sets the access 
permission to allow execute operations;
if false to disallow execute operations
Returns: true if and only if the operation succeeded.

如果用户没有更改此抽象路径名的访问权限的权限, 则该操作将失败。如果executable为false, 并且基础文件系统未实现执行权限, 则该操作将失败。

setReadable:

为该抽象路径名设置所有者的读取权限的便捷方法。

public boolean setReadable(boolean readable)
Parameters: readable - If true, sets the access permission to 
allow read operations; if false to disallow read operations
Returns: true if and only if the operation succeeded.

如果用户没有更改此抽象路径名的访问权限的权限, 则该操作将失败。如果可读为false, 并且基础文件系统未实现读取权限, 则该操作将失败。

setWritable:

一种方便的方法, 用于为此抽象路径名设置所有者的写许可权。

public boolean setWritable(boolean writable)
Parameters: writable - If true, sets the access permission
to allow write operations; if false to disallow write operations
Returns: true if and only if the operation succeeded.

如果用户没有更改此抽象路径名的访问权限的权限, 则该操作将失败。

// Java program to change the file permissions
import java.io.*;
  
public class Test
{
     public static void main(String[] args)
     {
         // creating a new file instance
         File file = new File( "C:\\Users\\Mayank\\Desktop\\1.txt" );
          
         // check if file exists
         boolean exists = file.exists();
         if (exists == true )
         {
             // changing the file permissions
             file.setExecutable( true );
             file.setReadable( true );
             file.setWritable( false );
             System.out.println( "File permissions changed." );
  
             // printing the permissions associated with the file currently
             System.out.println( "Executable: " + file.canExecute());
             System.out.println( "Readable: " + file.canRead());
             System.out.println( "Writable: " + file.canWrite());
              
         }
         else
         {
             System.out.println( "File not found." );
         }
     }
}

输出如下

File permissions changed.
Executable: true
Readable: true
Writable: false

参考文献:

  • 甲骨文

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: