Java文件类boolean canExecute()方法(带示例)

文件类boolean canExecute() (File Class boolean canExecute())

  • This method is available in package java.io.File.canExecute().

    软件包java.io.File.canExecute()中提供了此方法。

  • This method is used to execute the file and the file is represented by the abstract file path or in other words this method is used to test whether the application can execute the file or not.

    此方法用于执行文件,并且文件由抽象文件路径表示,或者换句话说,该方法用于测试应用程序是否可以执行文件。

  • The return type of this method is Boolean i.e. it returns true or false if true that means file can be executed by the application which is represented by the file path or in other words file already exists to execute and returns false that means file does not exist it means the application is not allowed to execute the file.

    此方法的返回类型为布尔值,即返回true或false,如果为true则表示文件可以由文件路径表示的应用程序执行,或者换句话说,文件已经存在要执行,并且返回false表示文件不存在这意味着不允许应用程序执行文件。

  • This method may raise an exception( i.e. Security Exception) if the execute access is not given to the file.

    如果未授予文件执行访问权限,则此方法可能引发异常(即Security Exception)。

Syntax:

句法:

    boolean canExecute(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is Boolean i.e. it returns true if the file already exists and allowed to execute the file which is denoted by the abstract file path returns false otherwise.

此方法的返回类型为Boolean,即如果文件已经存在并且允许执行该文件(由抽象文件路径表示),则返回true,否则返回false。

Java程序演示canExecute()方法的示例 (Java program to demonstrate example of canExecute() 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 ExecuteFile {
    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 canExecute() is allowed to execute 
            // the file if file is already exists and 
            // it returns true if file is executable else false returns.
            if (file1.canExecute())
                System.out.println("This file " + file1.getName() + " " + "is executable");
            else
                System.out.println("This file " + file1.getName() + " " + "is not executable");

            // By using canExecute() is not allowed to execute 
            // the file because this file is not already exists 
            // and it returns false.
            if (file2.canExecute())
                System.out.println("This file " + file2.getName() + " " + "is executable");
            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 ExecuteFile.java
D:\Programs>java ExecuteFile

This file C:\Users\computer clinic\OneDrive\Articles\myjava.txt is not executable
This file C:\Users\computer clinic\OneDrive\Articles\java.txt is not executable


翻译自: https://www.includehelp.com/java/file-class-boolean-canexecute-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值