Friday 13 January 2012

How to check File Permission in Java with Example - Java IO Tutorial

Java provides several methods to check file and directory permissions. In last couple of articles we have seen how to create File in java and how to read text file in Java and in this article we will learn how to check whether file is read only , whether file has write permission or not etc. In Java  we know we have file object to deal with Files  if we have created any file in our application using the file object , we have the privilege to check the access permission of that file using simple method of File class in Java. Let see what the methods are and how to use that method

How to check File Permission in Java with Example

How to find if File is ReadOnly in Java
boolean canRead() : this method is used to check that our application can have access to read the file or not. Here is an example of checking a file is read only in Java. If canRead() return true means file is read only in Java other wise file is protected and we can’t read it from Java or other Unix command.


Code Example of Checking Read Permission in Java
import java.io.File;

/**
 * @author Javin
 *
 */
public class CanReadTest {
       public static void main(String[] args) throws SecurityException {
        // Create a File object
        File myTestFile = new File("C:/Documents and Settings/dost/My Documents/canReadTest.txt");
        //Tests whether the application can Read  the file
        if (myTestFile.canRead()) {
            System.out.println(myTestFile.getAbsolutePath() + "Can Read: ");
        } else {
            System.out.println(myTestFile.getAbsolutePath() + " Cannot Read: ");
        }


    }

}


How to check if File or Directory has Write Permission in Java
boolean canWrite() : this method is used to check that our application can have access to write on  the file or not. Below example is for checking whether a File has write permission in java or not. If canWrite() returns true means file is writable in Java otherwise write permission is not for current user in operating system or from Java.


Code Example of checking Write permission on File in Java
import java.io.File;
public class CanWriteTest {
       public static void main(String[] args) throws SecurityException {
        // Create a File object
        File myTestFile = new File("C:/Documents and Settings/dost/My Documents/canWriteTest.txt");
        //Tests whether the application can Read  the file
        if (myTestFile.canWrite()) {
            System.out.println(myTestFile.getAbsolutePath() + "Can Write: ");
        } else {
            System.out.println(myTestFile.getAbsolutePath() + " Cannot Write: ");
        }


    }

}



How to see if File or Directory has execute permission in Java
boolean canExecute(): This method of File Class is used to check that our application can have access to execute  the file or not. This example shows how to find execute permission of File in Java. If canExecute() return true means current user has execute permission on file in java.


Code Example of Checking Execute permission in Java
import java.io.File;

public class CanExecuteTest {
       public static void main(String[] args) throws SecurityException  {
        // Create a File object
        File myTestFile = new File("C:/Documents and Settings/dost/My Documents/canExecuteTest.txt");
        //Tests whether the application can execute the file
        if (myTestFile.canExecute()) {
            System.out.println(myTestFile.getAbsolutePath() + "Can Execute: ");
        } else {
            System.out.println(myTestFile.getAbsolutePath() + " Cannot Execute: ");
        }
    }
}


Note: All this method can throw SecurityException if security manager exist and it denies to access of that file for that operation which we are performing on that

That’s all on checking File Permission in Java. We have see how to check read permission, write permission and execute permission from Java Code with Sample Example.

No comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More