How to set the file permission in Java?

Here ist an example to explain how to set the file permission in Java.

  • First Example with File permission

file.canExecute(); – return true, file is executable; false is not.
file.canWrite(); – return true, file is writable; false is not.
file.canRead(); – return true, file is readable; false is not.

file.setExecutable(boolean); – true, allow execute operations; false to disallow it.
file.setReadable(boolean); – true, allow read operations; false to disallow it.
file.setWritable(boolean); – true, allow write operations; false to disallow it.

package com.nourit.javaIO;

import java.io.File;

public class FilePermissionExample1 {

  public static void main(String[] args) {
    File file = new File("D:/tmp/nourit.txt");
    System.out.println("================================================");
    System.out.println("Has Permission Execute : " + file.canExecute());
    System.out.println("Has Permission Write : " + file.canWrite());
    System.out.println("Has Permission Read : " + file.canRead());

    file.setExecutable(false);
    file.setReadable(false);
    file.setWritable(false);
    System.out.println("================================================");
    System.out.println("Permission Execute : " + file.canExecute());
    System.out.println("Permission Write   :  " + file.canWrite());
    System.out.println("Permission Read    : " + file.canRead());
    System.out.println("================================================");
  }
}
  • Second Example: How to set Execute Permission of a File in java

boolean setExecutable(boolean executable) permission of a file/directory for the owner only.
boolean setExecutable(boolean executable, boolean ownerOnly) permission of a file/directory for the owner or everybody

package com.nourit.javaIO;

import java.io.File;


public class FilePermissionExample2 {
  public static void main(String[] args) {
    
    File file = new File("D:\\nour-it.txt");
    if (file.exists()) {
      // Setting execute permission for owner only
      boolean result = file.setExecutable(true);
      System.out.println("Execute permission for owner successfully? " + result);
    } else {
      System.out.println("File doesn't exist.");
    }
    
    if (file.exists()) {
      // Setting execute permission for owner only
      boolean result = file.setExecutable(true, true);
      System.out.println("Execute permission owner only successfully? " + result);
    } else {
      System.out.println("File doesn't exist.");
    }

    if (file.exists()) {
      // Setting execute permission for all
      boolean result = file.setExecutable(true, false);
      System.out.println("Execute permission for all successfully? " + result);
    } else {
      System.out.println("File doesn't exist.");
    }
    
  }
}
  • Third Example: explain how to set read Permission of a File in Java

boolean setReadable(boolean readable) permission of a file/directory for the owner only.
boolean setReadable(boolean readable, boolean ownerOnly) permission of a file/directory for the owner or everybody

package com.nourit.javaIO;

import java.io.File;

public class FilePermissionExample3 {

  public static void main(String[] args) {
    File file = new File("D:\\nour-it.txt");
    if (file.exists()) {
      // Setting read permission for owner only
      boolean result = file.setReadable(true);
      System.out.println("Read permission for owner successfully: " + result);
    } else {
      System.out.println("File doesn't exist.");
    }
    
    if (file.exists()) {
      // Setting read permission  owner only
      boolean result = file.setReadable(true, true);
      System.out.println("Read permission for owner successfully: " + result);
    } else {
      System.out.println("File doesn't exist.");
    }


    if (file.exists()) {
      // Setting read permission for all
      boolean result = file.setReadable(true, false);
      System.out.println("Read permission ffor all successfully: " + result);
    } else {
      System.out.println("File doesn't exist.");
    }
  }
}
  • Fourth Example: explain how to set write permission of a file in java

boolean setWritable(boolean writable) permission of a file/directory for the owner only.
boolean setWritable(boolean writable, boolean ownerOnly) boolean ownerOnly) permission of a file/directory for the owner or everybody.

package com.nourit.javaIO;

import java.io.File;

public class FilePermissionExample4 {

  public static void main(String[] args) {
    File file = new File("D:\\nour-it.txt");
    if (file.exists()) {
      // Setting write permission for owner only
      boolean result = file.setWritable(true);
      System.out.println("write permission for owner successfully: " + result);
    } else {
      System.out.println("File doesn't exist.");
    }
    
    if (file.exists()) {
      // Setting write permission  owner only
      boolean result = file.setWritable(true, true);
      System.out.println("write permission for owner successfully: " + result);
    } else {
      System.out.println("File doesn't exist.");
    }

    if (file.exists()) {
      // Setting write permission for all
      boolean result = file.setWritable(true, false);
      System.out.println("write permission ffor all successfully: " + result);
    } else {
      System.out.println("File doesn't exist.");
    }
  }

}

 

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*