File Permissions in Java

Java provides a number of method calls to check and change the permission of a file, such as a read-only file can be changed to have permissions to write. File permissions are required to be changed when the user want to restrict the operations permissible on a file. For example, a file permission can be changed from write to read-only because the user no longer want to edit the file.

Checking the current file permissions


A file can be in any combination of following permissible permissions:

  • Executable: Tests whether the application can execute the file denoted by this abstract path name.
    Syntax:
    public boolean canExecute()
    Returns: true if and only if the abstract path name
    exists and the application is allowed to execute the file
  • Readable: Tests whether the application can read the file denoted by this abstract path name.
    Syntax:

    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
  • Writable: Tests whether the application can modify the file denoted by this abstract path name.
    Syntax:
    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.

For example, a file can be readable and writable but not executable. Here’s Java program to get the current permissions associated with a file.

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

Output

Executable: true
Readable: true
Writable: true

Changing file permissions


A file can have any combinations of the following permissions:

  • Executable
  • Readable
  • Writable

Here are methods to change the permissions associated with a file:

  • setExecutableA convenience method to set the owner’s execute permission for this abstract path name.
    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. 

    The operation will fail if the user does not have permission to change the access permissions of this abstract path name. If executable is false and the underlying file system does not implement an execute permission, then the operation will fail.

  • setReadable: A convenience method to set the owner’s read permission for this abstract path name.
    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. 

    The operation will fail if the user does not have permission to change the access permissions of this abstract path name. If readable is false and the underlying file system does not implement a read permission, then the operation will fail.

  • setWritable : A convenience method to set the owner’s write permission for this abstract path name.
    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.

    The operation will fail if the user does not have permission to change the access permissions of this abstract path name.

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

Output

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

References:

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.