Wednesday, May 24, 2023

How to Change File Permissions in Java – Example Tutorial

Hello guys, In the last article, we saw how to check whether an application can access the file and perform any read-write or execute an operation on that file by using the inbuilt method provided by File Object. Now we deal with some more methods of file class which will use to provide some privileges to users so that they can perform read, write, and execute operations on the particular file. There are few more methods added with the new File API in Java 7, but in this tutorial, we will only learn about traditional ways to change the permission of a file from the Java program, which should work on all versions of JDK.


How to set Execute Permission on File in Java

 boolean setExecutable(boolean exe, boolean owneronly) : This method is used to set the execute permission for the owner  of the file and also we can provide every user execute permission using this method, if the operation is successful then this method returns true.

This method is overloaded in this class if we want to provide execute permission only to the owner we can also use method  boolean setExecutable(boolean exe).

Here is a code example of setting or changing execute permission on File in Java. you can also change execute permission on directory similarly. In Unix, if a directory doesn't have to execute permission means you can not go inside that directory.









import java.io.File;
public class SetExecuteTest{

       public static void main(String[] args)throws SecurityException {

        File file = new File("C:/setExecuteTest.txt");

        if (file.exists()) {
            boolean bval = file.setExecutable(true);
            System.out.println("set the owner's execute permission: "+ bval);
        } else {
            System.out.println("File cannot exists: ");
        }

       if (file.exists()) {
            boolean bval = file.setExecutable(true,false);
            System.out.println("set the everybody execute permission: "+ bval);
        } else {
            System.out.println("File cannot exists: ");
        }
    }
}




Btw, if you don't remember all the file permission or forgot how to make a file read only or only group access, here is a nice diagram to recap what are different file permissions in UNIX or Linux environment:

How to Change File Permissions in Java – Example Tutorial





How to set Write Permission on File in Java

how to change or set file permissions in java code exampleboolean setWriteable(boolean write,boolean owneronly) : This method is used to set the write permission for the owner of the file, and also we can provide every user write permission using this method ,if the operation is successful then this method returns true. 

This method is overloaded and if we want to provide write permission only to the owner of the file we can use instead boolean setWritable(boolean write).

here is a code example of setting write permission to a file in Java. same code can also be used to change write permission from a Java File.

import java.io.File;


public class SetWritableTest{

       public static void main(String[] args)throws SecurityException {

        File file = new File("C:/setWriteableTest.txt");
        
        //set write permission on file only for owner
        if (file.exists()) {
            boolean bval = file.setWritable(true);
             System.out.println("set the owner's write permission: "+ bval);
        } else {
             System.out.println("File cannot exists: ");
        }

        //Set write permission on File for all.
        if (file.exists()) {
            boolean bval = file.setWritable(true,false);
            System.out.println("set the every user write permission: "+ bval);
        } else {
            System.out.println("File cannot exists: ");
        }


    }
}

How to set Read Permission on File in Java

boolean setReadable(boolean read,boolean owneronly) : This method is used to set the read permission for the owner of the file and also we can provide every user read permission using this method,if the operation is successful then this method returns true. This method is overloaded and if we want to provide read permission only to the owner we can use instead boolean setReadable(boolean read).

here is a complete code example to set write permission on File in Java. this code can also be used change write permission of a file in Java.

import java.io.File;
public class SetReadableTest{

       public static void main(String[] args)throws SecurityException {

        File file = new File("C:/setReadableTest.txt");
        if (file.exists()) {

            boolean bval = file.setReadable(true);
            System.out.println("set the Owner Read permission: "+ bval);
        } else {
            System.out.println("File cannot exists: ");
        }

       if (file.exists()) {
            boolean bval = file.setReadable(true,false);
            System.out.println("set the every user Read permission: "+ bval);
        } else {
            System.out.println("File cannot exists: ");
        }
    }
}


How to make a directory read-only in Java

boolean setReadOnly() : This method is used to make the file or directory read-only if we call this method on any file then no write operation can not be performed on that file.

import java.io.*;

public class SetReadOnlyTest{
      public static void main(String[] args)throws SecurityException {
           
        File file = new File("C:/setReadOnlyTest.txt");
        if (file.exists()) {
            boolean bval = file.setReadOnly();
            System.out.println("Read opearation is permitted: "+bval);
        } else {
            System.out.println("File cannot exists: ");
        }

    }
}


Related Java Tutorial


1 comment :

Anonymous said...

But we can change attribute of file in windows os by right click and change attribute by unmark read only.
Is there any way in java so the file can be permanently read only (no one can change that read only attribute unless by java )?

Post a Comment