📜  Java中的文件权限

📅  最后修改于: 2022-05-13 01:54:30.159000             🧑  作者: Mango

Java中的文件权限

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

检查当前文件权限

文件可以是以下表格格式的方法描述的以下允许权限的任意组合/

MethodAction Performed
canExecutable()Returns true if and only if the abstract pathname exists and the application is allowed to execute the file
canRead()Tests whether the application can read the file denoted by this abstract pathname
canWrite()Returns true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise

实现:文件可读写但不可执行。这是一个获取与文件关联的当前权限的Java程序。

例子:

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 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中的文件可以具有以下权限的任意组合:

  • 可执行文件
  • 可读
  • 可写

以下是更改与文件关联的权限的方法,如下面的表格格式所示:

MethodAction Performed
setExecutable()Set the owner’s execute permission for this abstract pathname
setReadable()Set the owner’s read permission for this abstract pathname
setWritable()Set the owner’s write permission for this abstract pathname

例子:

Java

// 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");
        }
    }
}

输出: