Showing posts with label Change File Permission. Show all posts
Showing posts with label Change File Permission. Show all posts

Change file permission using java

 
Changing file permission using GUI is an end user easy operation. Same thing if we need to change multiple files like around hundreds or thousands of file then its difficult. But we can achieve this easy by using Java code. So lets see simple example for reading file permissions and then changing file to full permission (Read, Write and Execute).
Change file permission using java



import java.io.File;

public class FilePermissionTest {

 public static void main(String[] args) {
  
  String testFile = "D://testfile.txt";
  
  File file = new File(testFile);
  System.out.println("Current File Permissions : ");
  System.out.println("\t Read     : "+file.canRead());
  System.out.println("\t Write    : "+file.canWrite());
  System.out.println("\t Execute  : "+file.canExecute());
  
  System.out.println("\nLets change file to full permission ");
  
  file.setReadable(true);
  file.setWritable(true);
  file.setExecutable(true);
  
  System.out.println("\nChanged File Permissions : ");
  System.out.println("\t Read     : "+file.canRead());
  System.out.println("\t Write    : "+file.canWrite());
  System.out.println("\t Execute  : "+file.canExecute());
 }
}

OUTPUT:


Current File Permissions : 
  Read     : true
  Write    : false
  Execute  : true

Lets change file to full permission 

Changed File Permissions : 
  Read     : true
  Write    : true
  Execute  : true