Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

File Permissions in Java

  • Difficulty Level : Easy
  • Last Updated : 03 Jan, 2022

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 wants to restrict the operations permissible on a file. For example, file permission can be changed from write to read-only because the user no longer wants to edit the file. 

Checking the Current File Permissions

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

1. Executable: Tests whether the application can execute the file denoted by this abstract path name. 

Syntax: 

public boolean canExecute()

Returns: It returns true if and only if the abstract pathname exists and the application is allowed to execute the file

2. Readable: Tests whether the application can read the file denoted by this abstract path name. 

Syntax: 

public boolean canRead()

Returns: It returns true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise

3. Writable: Tests whether the application can modify the file denoted by this abstract path name. 

Syntax:

public boolean canWrite()

Returns: It 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.

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

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

Output 

Executable: true
Readable: true
Writable: true

Changing File Permissions

A file can have any combination of the following permissions: 

  • Executable
  • Readable
  • Writable

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

1. setExecutableA convenience method to set the owner’s execute permission for this abstract path name. 

Syntax:

public boolean setExecutable(boolean executable)

Parameters: executable – If true, sets the access permission to allow execute operations; if false to disallow execute operations.

Returns: It 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 pathname. If the executable is false and the underlying file system does not implement an execute permission, then the operation will fail. 

2. setReadable: A convenience method to set the owner’s read permission for this abstract path name. 

Syntax:

public boolean setReadable(boolean readable)

Parameters: readable – If true, sets the access permission to allow read operations; if false to disallow read operations

Returns: It 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.

3. setWritable : A convenience method to set the owner’s write permission for this abstract path name. 

Syntax:

public boolean setWritable(boolean writable)

Parameters: writable – If true, sets the access permission to allow write operations; if false to disallow write operations

Returns: It 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




// 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

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!