Nov 9, 2013

Read, Write and Execute permissions in Java - Get and Set File permission

In Java File class includes methods related to getting permission and setting permission of file/directory.
Get file permission - Read, write and Execute 
file.canExecute() - Tests whether the application can execute the file or not, returns true if Execute operation is allowed.
file.canRead() - Tests whether the application can read the file or not,returns true if Read operation is allowed.
file.canWrite()- Tests whether the application can modify the file or not,returns true if Write operation allowed.

Set file permission - Read, Write and Execute 

file.setReadOnly(boolean readable, boolean ownerOnly) / file.setReadOnly(boolean readable) Set read only permission to owner only or others too (By default, ownerOnly is true)
file.setWritable(boolean writable, boolean ownerOnly) /file.setWritable(boolean writable) -Set Write only permission to owner only or others too (By default, ownerOnly is true)
file.setExecutable(boolean executable, boolean ownerOnly)/file.setExecutable(boolean executable) -Set executable permission to owner only or others too (By default, ownerOnly is true)

Note:-
For all above methods, if ownerOnly argument is false, then permission is granted for all others along with .
Below sample program is executed in Linux environment and permissions on the given file is verified using shell command.
import java.io.File;

public class FilePermissionManipulation {

 public static void main(String[] args) {

  File file = new File(

    "/home/zytham/Downloads/org.jacoco.agent-0.7.2.201409121644-runtime.jar");

  System.out.println("File permission info");

  System.out.println("Read || Write || Execte");

  System.out.println(file.canRead() + " ||  " + file.canWrite()

    + "  ||  " + file.canExecute());



  /* Modify file permission using Java API */

  // execute permission not allowed for any user(second arg is false)

  file.setExecutable(false, false);

  // Write permission is allowed only to owner, second arg is missing

  file.setWritable(true);

  // Read allowed for all, second arg is false

  file.setExecutable(true, false);


  System.out.println("File permission after modification");

  System.out.println("Read || Write || Execte");

  System.out.println(file.canRead() + " ||  " + file.canWrite()

    + "  ||  " + file.canExecute());
 }

}
===============Sample output===============
zytham@ubuntu:~/Downloads$ ls -l org.jacoco.agent-0.7.2.201409121644-runtime.jar
-r-x--x--x 1 zytham zytham 286862 Oct 10 06:10 org.jacoco.agent-0.7.2.201409121644-runtime.jar

zytham@ubuntu:~/java$ java FilePermissionManipulation
File permission info
Read || Write || Execte
true ||  false  ||  true
File permission after modification
Read || Write || Execte
true ||  true  ||  true

zytham@ubuntu:~/Downloads$ ls -l org.jacoco.agent-0.7.2.201409121644-runtime.jar
-rwx--x--x 1 zytham zytham 286862 Oct 10 06:10 org.jacoco.agent-0.7.2.201409121644-runtime.jar
=========================================
From above sample output, we notice that write is not allowed on jar file.After executing setWritable(true), write permission is allowed on it(as highlighted -rwx).
Location: Hyderabad, Telangana, India