Java File 权限

Java File 权限

Java提供了一些方法调用来检查和改变文件的权限,比如一个只读的文件可以被改变为有写入的权限。当用户想限制一个文件所允许的操作时,就需要改变文件权限。例如,文件权限可以从写改为只读,因为用户不再想编辑该文件。

检查当前的文件权限

一个文件可以处于以下允许的权限的任何组合中,以下是以表格形式描述的方法/。

方法 执行的操作
canExecutable() 当且仅当抽象路径名存在并且应用程序被允许执行该文件时,返回true。
canRead() 测试应用程序是否可以读取这个抽象路径名所表示的文件。
canWrite() 当且仅当文件系统实际包含该抽象路径名所表示的文件,并且应用程序被允许向该文件写入时,返回true;否则返回false。

实现: 一个文件可以是可读可写的,但不能执行。下面是一个Java程序,用来获取与一个文件相关的当前权限。

例子

// Java Program to Check the Current File Permissions
 
// Importing required classes
import java.io.*;
 
// Main class
public class Test {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a file by
        // creating object of File class
        File file
            = new File("C:\\Users\\Mayank\\Desktop\\1.txt");
 
        // Checking if the file exists
        // using exists() method of File class
        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());
        }
 
        // If we enter else it means
        // file does not exists
        else {
            System.out.println("File not found.");
        }
    }
}
Java

输出

Java中的文件权限

改变文件权限

Java中的一个文件可以有以下权限的任何组合

  • 可执行
  • 可读
  • 可写

下面是改变与文件相关的权限的方法,以表格的形式描述如下。

方法 执行的操作
setExecutable() 为这个抽象路径名设置所有者的执行许可。
setReadable() 设置所有者对该抽象路径名的读取权限。
setWritable() 设置所有者对该抽象路径名的写入权限。

注意

  • setReadable() 操作 ,如果用户没有权限改变这个抽象路径名的访问权限,则会失败。如果可读性为false,并且底层文件系统没有实现读取权限,那么该操作将失败。
  • setWritable() 如果用户没有权限改变这个抽象路径名的访问权限,操作将失败。

例子

// Java Program to Change File Permissions
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a new file by
        // creating object of File class where
        // local directory is passed as in argument
        File file
            = new File("C:\\Users\\Mayank\\Desktop\\1.txt");
 
        // Checking 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());
        }
 
        // If we reach here, file is not found
        else {
            System.out.println("File not found");
        }
    }
}
Java

输出

Java中的文件权限

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册