java中execute用法_Java File canExecute()用法及代码示例

canExecute()函数是Java中File类的一部分。此函数确定程序是否可以执行由抽象路径名表示的指定文件。如果文件路径存在并且允许应用程序执行文件,则此方法将返回true。否则它将返回false。

函数签名:

public boolean canExecute()

用法:

file.canExecute();

参数:该函数不接受任何参数。

返回值:此函数返回一个布尔值,表示是否可以执行指定的文件。

异常:如果对文件的读取访问被拒绝,则此方法将引发Security Exception

以下示例程序旨在说明canExecute()函数的用法:

范例1:文件“F:\\program.txt”是F:目录中的现有文件,并且允许该程序执行该文件的权限。

// Java program to demonstrate

// canExecute() method of File class

import java.io.*;

public class solution {

// Driver Code

public static void main(String args[])

{

// Get the file to be executed

File f = new File("F:\\program.txt");

// Check if this file

// can be executed or not

// using canExecute() method

if (f.canExecute()) {

// The file is can be executed

// as true is returned

System.out.println("Executable");

}

else {

// The file is cannot be executed

// as false is returned

System.out.println("Non Executable");

}

}

}

输出:

Executable

范例2:文件“F:\\program1.txt”不存在,我们将尝试检查该文件是否可执行。

// Java program to demonstrate

// canExecute() method of File class

import java.io.*;

public class solution {

// Driver Code

public static void main(String args[])

{

// Get the file to be executed

File f = new File("F:\\program1.txt");

// Check if this file

// can be executed or not

// using canExecute() method

if (f.canExecute()) {

// The file is can be executed

// as true is returned

System.out.println("Executable");

}

else {

// The file is cannot be executed

// as false is returned

System.out.println("Non Executable");

}

}

}

输出:

Non Executable

注意:程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的路径。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值