Lets discuss the file permission concept in java i/o package
Below are the list of permission related functions in File Class
Below are the list of permission related functions in File Class
boolean | canExecute() Tests whether the application can execute the file denoted by this abstract pathname. |
boolean | canRead() Tests whether the application can read the file denoted by this abstract pathname. |
boolean | canWrite() Tests whether the application can modify the file denoted by this abstract pathname. |
Lets understand this with the example below
package com.java.everythingaboutselenium;
import java.io.File;
import java.io.IOException;
public class FilePermission
{
public static void main( String[] args )
{
try {
File f = new File("/selenium/execute.sh");
if(f.exists()){
System.out.println("Is Execute allow : " + f.canExecute());
System.out.println("Is Write allow : " + f.canWrite());
System.out.println("Is Read allow : " + f.canRead());
}
f.setExecutable(false);
f.setReadable(false);
f.setWritable(false);
System.out.println("Is Execute allow : " + f.canExecute());
System.out.println("Is Write allow : " + f.canWrite());
System.out.println("Is Read allow : " + f.canRead());
if (f.createNewFile()){
System.out.println("File is created!");
}else{
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}