Home » Java programming language

Java File Class boolean setExecutable(boolean exec_file) method with Example

Java File Class boolean setExecutable(boolean exec_file) method: Here, we are going to learn about the boolean setExecutable(boolean exec_file) method of File class with its syntax and example.
Submitted by Preeti Jain, on July 07, 2019

File Class boolean setExecutable(boolean exec_file)

  • This method is available in package java.io.File.setExecutable().
  • This method is used to set the access permission to owner’s or everybody's to execute the file.
  • The return type of this method is boolean so the expected value will be true or false if returns true that means file is executable else file is not executable.
  • This method may raise an exception( i.e. Security Exception) if the file written permission is not given.
  • This method is overridable and first overridable method accepts one parameter and other accepts two-parameter.

Syntax:

    boolean setExecutable(Boolean exec_file){
    }

Parameter(s):

We pass only one object as a parameter in the method of the File i.e exec_file(Executable file ).

Return value:

The return type of this method is Boolean if it returns true so the file is executable and else returns false file is not executable.

Java program to demonstrate example of setExecutable() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because 
// it may raise an exception when working with files
import java.lang.Exception;

public class ExecutableFileClass {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\java.txt");

            // By using setExecutable(true) is allowed to execute 
            // the file because we passed true as a parameter in the method.
            if (file1.setExecutable(true))
                System.out.println("This file " + file1.getName() + " " + "is executable  because access permission is given");
            else
                System.out.println("This file " + file1.getName() + " " + "is not executable because access permission is not given");

            // By using setExecutable(false) is not allowed to execute 
            // the file because we passed false as a parameter 
            //in the method of the file.
            if (file2.setExecutable(false))
                System.out.println("This file " + file2.getName() + " " + "is executable because access permission is given");
            else
                System.out.println("This file " + file2.getName() + " " + "is not executable because access permission is not given");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac ExecutableFileClass.java

D:\Programs>java ExecutableFileClass
This file myjava.txt is executable because access permission is given
This file java.txt is not executable because access permission is not given


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.