Java.io.File.canExecute() Method


Advertisements


Description

The java.io.File.canExecute() method returns true if the file can be executed by its abstract name.

Declaration

Following is the declaration for java.io.File.canExecute() method −

public boolean canExecute()

Parameters

NA

Return Value

This method returns boolean value. True, if the path name exists and the file is allowed to be executed by the application.

Exception

SecurityException − If the file is not allowed to be executed.

Example

The following example shows the usage of java.io.File.canExecute() method.

package com.tutorialspoint;

import java.io.File;

public class FileDemo {

   public static void main(String[] args) {      
      File f = null;
      String[] strs = {"test.txt", "/test.txt"};
      
      try {
      
         // for each string in string array 
         for(String s:strs ) {
         
            // create new file
            f = new File(s);
            
            // true if the file is executable
            boolean bool = f.canExecute();
            
            // find the absolute path
            String a = f.getAbsolutePath(); 
            
            // prints absolute path
            System.out.print(a);
            
            // prints
            System.out.println(" is executable: "+ bool);
         } 
         
      } catch(Exception e) {
      
         // if any I/O error occurs
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

D:\EclipseAndroid\IO\BufferedInputStream\test.txt is executable: true
D:\test.txt is executable: false

java_io_file.htm

Advertisements