Java provides several methods to check file and directory permissions. In the last couple of articles, we have seen how to create Files in java and how to read a text file in Java, and in this article, we will learn how to check whether the file is read-only, whether the file has to write permission or not, etc. In Java we know we have a 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 a 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 Read Only 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 that is read-only in Java. If canRead() return true means file is read-only in Java otherwise file is protected and we can’t read it from Java or other Unix command.
And, If you are new to the Java world then I also recommend you go through these Java programming online courses to learn Java in a better and more structured way. This is a list of the best and up-to-date courses to learn Java online.
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. The 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 the 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: ");
}
}
}
And, if you don't remember what does different file permissions means, here is a nice diagram to recap file permissions in Linux or UNIX environment:
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 the 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 these methods can throw SecurityException if a security manager exists and it denies to access that file for that operation which we are performing on that
That’s all on checking File Permission in Java. We have seen how to check read permission, write permission, and execute permission from Java Code with Sample Example.
Some other Tutorial you may like
In java CanRead and CanWrite always return true for windows box. No matter whatever the permission set by OS.
ReplyDelete