Easy methods to set Execute Permission on File in Java
boolean setExecutable(boolean exe, boolean owneronly) : This technique is used to set the execute permission for the proprietor of the file and in addition we are able to present each person execute permission utilizing this technique, if the operation is profitable then this technique returns true.
Here’s a code instance of setting or altering execute permission on File in Java. you may as well change execute permission on listing equally. In Unix, if a listing does not must execute permission means you can’t go inside that listing.
import java.io.File;
public class SetExecuteTest{
public static void essential(String[] args)throws SecurityException {
File file = new File(“C:/setExecuteTest.txt”);
if (file.exists()) {
boolean bval = file.setExecutable(true);
System.out.println(“set the proprietor’s execute permission: “+ bval);
} else {
System.out.println(“File can not exists: “);
}
if (file.exists()) {
boolean bval = file.setExecutable(true,false);
System.out.println(“set the everyone execute permission: “+ bval);
} else {
System.out.println(“File can not exists: “);
}
}
}
Btw, should you do not keep in mind all of the file permission or forgot the best way to make a file learn solely or solely group entry, here’s a good diagram to recap what are totally different file permissions in UNIX or Linux surroundings:
Easy methods to set Write Permission on File in Java
This technique is overloaded and if we wish to present write permission solely to the proprietor of the file we are able to use as an alternative boolean setWritable(boolean write).
here’s a code instance of setting write permission to a file in Java. identical code can be used to alter write permission from a Java File.
import java.io.File;
public class SetWritableTest{
public static void essential(String[] args)throws SecurityException {
File file = new File(“C:/setWriteableTest.txt”);
//set write permission on file just for proprietor
if (file.exists()) {
boolean bval = file.setWritable(true);
System.out.println(“set the proprietor’s write permission: “+ bval);
} else {
System.out.println(“File can not exists: “);
}
//Set write permission on File for all.
if (file.exists()) {
boolean bval = file.setWritable(true,false);
System.out.println(“set the each person write permission: “+ bval);
} else {
System.out.println(“File can not exists: “);
}
}
}
Easy methods to set Learn Permission on File in Java
boolean setReadable(boolean learn,boolean owneronly) : This technique is used to set the learn permission for the proprietor of the file and in addition we are able to present each person learn permission utilizing this technique,if the operation is profitable then this technique returns true. This technique is overloaded and if we wish to present learn permission solely to the proprietor we are able to use as an alternative boolean setReadable(boolean learn).
here’s a full code instance to set write permission on File in Java. this code can be used change write permission of a file in Java.
import java.io.File;
public class SetReadableTest{
public static void essential(String[] args)throws SecurityException {
File file = new File(“C:/setReadableTest.txt”);
if (file.exists()) {
boolean bval = file.setReadable(true);
System.out.println(“set the Proprietor Learn permission: “+ bval);
} else {
System.out.println(“File can not exists: “);
}
if (file.exists()) {
boolean bval = file.setReadable(true,false);
System.out.println(“set the each person Learn permission: “+ bval);
} else {
System.out.println(“File can not exists: “);
}
}
}
Easy methods to make a listing read-only in Java
boolean setReadOnly() : This technique is used to make the file or listing read-only if we name this technique on any file then no write operation can’t be carried out on that file.
import java.io.*;
public class SetReadOnlyTest{
public static void essential(String[] args)throws SecurityException {
File file = new File(“C:/setReadOnlyTest.txt”);
if (file.exists()) {
boolean bval = file.setReadOnly();
System.out.println(“Learn opearation is permitted: “+bval);
} else {
System.out.println(“File can not exists: “);
}
}
}
Associated Java Tutorial