group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
254 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
06 Feb 2019

How to set file permissions in java?

java.io.File class has six important methods which are used to set file permissions in java. All these methods are introduced in JDK 1.6. Let’s see these methods in detail first.

1) boolean setExecutable(boolean executable)

This method sets the execute permission of a file/directory for the owner only.

2) boolean setExecutable(boolean executable, boolean ownerOnly)

This method sets the execute permission of a file/directory for the owner or everybody. If the second argument is true, then it sets the execute permission for owner only. If it is false, then it sets the execute permission for all.

3) boolean setReadable(boolean readable)

This method sets the read permission of a file/directory for the owner only.

4) boolean setReadable(boolean readable, boolean ownerOnly)

This method sets the read permission of a file/directory for the owner or everybody. If you pass true as second argument to this method, then it will set read permission for owner only. If you pass false as second argument, then it sets read permission for everyone.

5) boolean setWritable(boolean writable)

This method sets write permission of a file/directory for owner only.

6) boolean setWritable(boolean writable, boolean ownerOnly)

This method sets the write permission of a file/directory for owner or everyone. If you pass true as second argument to this method, then it sets write permission for owner only. If you pass false as second argument, then it sets write permission for all.

AdhikariHarika Reshma
AdhikariHarika Reshma
Computer Science
1mo ago

File permissions in Java:Java provides a number of method calls to check and change 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 this file.

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 is java program to get the current permissions associated with a file.

import java.io.*;

public class Test

{  

   public static void main (Strings args[])

    File file=new File("c:\\Users\\xyyyzyy\\1.txt");

boolean exists=file.exists();

if(exists==true)

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

}

}

}

We can also change the file permissions:setExecutable,setReadable,setWritable

Shruthi Pillai
Shruthi Pillai
Information Technology
1mo ago

java.io.File class has six important methods which are used to set file permissions in java 

For Checking the File Permissions use: 

  • canExecute() : true if and only if the abstract pathname exists and the application is allowed to execute the file
  • canWrite() : 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.
  • canRead() : true if and only if the file specified by this abstract pathname exists and can be read by the application false otherwise

For Setting the File Permissions use: 

  • setExecutable() :  If true, sets the access permission to allow execute operations; if false to disallow execute operations
  • setWritable() :  If true, sets the access permission to allow write operations; if false to disallow write operations
  • setReadable() :  If true, sets the access permission to allow read operations; if false to disallow read operations