File canExecute() method in Java with Examples
The canExecute() function is a part of File class in Java. This function determines whether the program can execute the specified file denoted by the abstract pathname. If the file path exists and the application is allowed to execute the file, this method will return true. Else it will return false.
public boolean canExecute()
Parameters: This function does not accept any parameter.
Return Value: This function returns a boolean value representing whether the specified file can be executed or not.
Exceptions: This method throws Security Exception if the read access to the file is denied
Below programs illustrates the use of canExecute() function:
Example 1: The file “F:\\program.txt” is an existing file in F: directory and the program is allowed the permission to execute the file.
Example 2: The file “F:\\program1.txt” does not exist we will try to check if the file is executable or not.
Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.