Java File.setExecutable(boolean executable, boolean ownerOnly) example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to set the owner's or everybody's execute permission for this abstract pathname. Java File.setExecutable() method returns true if and only if the operation succeeded.

This method sets the owner's or everybody's execute permission for this abstract pathname.

syntax

public boolean setExecutable(boolean executable, boolean ownerOnly)

Parameters

executable - If true, sets the access permission to allow execute operations; if false to disallow execute operations.

ownerOnly - If true, the execute permission applies only to the owner's execute permission; otherwise, it applies to everybody.

Returns

true if and only if the operation succeeded. The operation will fail if the user does not have permission to change the access permissions of this abstract pathname.

Throws

SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the file.

Java File setExecutable(boolean executable, boolean ownerOnly) example

  • Create File instance new File("C:\\technicalkeeda\\hello.txt");
  • file.setExecutable(true, false) Set the file to executable by keeping true.
  • file.canExecute() Check if it can be executable or not.
package com.technicalkeeda;

import java.io.File;

public class App {

    public static void main(String[] args) {

        File file = new File("C:\\technicalkeeda\\hello.txt");

        System.out.println(file + " :- Is this file set to executable ?  " + file.setExecutable(true, false));

        System.out.println("Can you execute this file ? " + file.canExecute());

    }

}

output

C:\technicalkeeda\hello.txt :- Is this file set to executable ?  true

Can you execute this file ? true


© technicalkeeda.com 2018

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0442 seconds.