Home » Java programming language

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

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

File Class boolean setExecutable(boolean exec_file , boolean owner_access)

  • This method is available in package java.io.File.setExecutable(boolean exec_file , boolean owner_access).
  • This method is used to set the access permission to owner's or everybody's to execute the file.
  • This method accepts two parameters the first parameter is for file access and another parameter is for file is accessible by owner only or everybody's (i.e. If we pass true as a first parameter that means file is accessible else false file is not accessible and If we pass true as a second parameter that means file is accessible by owner's only else false that's means file is accessible to everybody's).
  • This method may raise an exception(i.e. Security Exception) if the file written permission is not given.

Syntax:

    boolean setExecutable(Boolean exec_file , boolean owner_access){
    }

Parameter(s):

We pass two objects as a parameter in the method of the File i.e exec_file(Executable file ) and owner_access( File is executable by the owner or everybody's).

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\\myjava1.txt");

            // By using setExecutable(true , true)  is allowed 
            // to execute the file by the owner only 
            // [i.e. First true is for whether file is executable or not 
            // (i.e. true means it is executable) and Second true is 
            // for whether file is accessible by owner or everbody's
            // (i.e. true means file is executable by owner only) ]
            if (file1.setExecutable(true, true))
                System.out.println("This file " + file1.getName() + " " + "is executable  by the owner only");
            else
                System.out.println("This file " + file1.getName() + " " + "is not executable");

            // By using setExecutable(true , false)  is allowed 
            // to execute the file by the everybody's 
            // [i.e. First true is for whether file is executable or not 
            // (i.e. true means it is executable) and Second true is 
            // for whether file is accessible by owner or everybody's 
            // (i.e. false means file is executable by everybody's) ]
            if (file2.setExecutable(true, false))
                System.out.println("This file " + file2.getName() + " " + "is executable by everybody's");
            else
                System.out.println("This file " + file2.getName() + " " + "is not executable");
        } 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  by the owner only
This file myjava1.txt is executable by everybody's





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.