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
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
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
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
Some other Tutorial you may like