Rabu, 26 Maret 2014

How to Change or Set File Permissions in Java – Code Example Tutorial






Change File Permissions in Java Code Example




In Last article we saw that how to check  whether any application can access the file and perform any read write or execute operation on that file by using the in built method provided by File Object. Now we deal with some more methods of file class which will used to provide some privileges to user so that they can perform read, write and execute operation on the particular file.








How to set Execute Permission on File in Java



 how to change or set file permissions in java code exampleboolean 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 operation is successful then this method returns true.





This method is overloaded in this class if we want to provide execute permission only 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 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: ");


        }




    }


}













How to set Write Permission on File in Java



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





































Source:http://javarevisited.blogspot.com/2012/01/change-file-permissions-java-change.html

1 komentar:

  1. Hi,
    Very useful article. Thanks for sharing.

    http://www.flowerbrackets.com/create-file-java-program/

    BalasHapus