Java File.setExecutable(boolean executable) example

 Posted On  | Pooja Lakkihal 

In this java tutorial, You will learn how to get the execute permission for the abstract path name. Java File.setExecutable() method returns true if and only if the operation succeeded.

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

syntax

public boolean setExecutable(boolean executable)

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. If executable is false and the underlying file system does not implement an excute permission, then the operation will fail.

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) example

  • Create File instance new File("C:\\technicalkeeda\\hello.txt");
  • file.setExecutable(true) 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));

        System.out.println(file + " :- Is this file executable ?  " + 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.0453 seconds.