java file.canexecute_Java File canExecute()用法及代碼示例
canExecute()函數(shù)是Java中File類的一部分。此函數(shù)確定程序是否可以執(zhí)行由抽象路徑名表示的指定文件。如果文件路徑存在並且允許應(yīng)用程序執(zhí)行文件,則此方法將返回true。否則它將返回false。
函數(shù)簽名:
public boolean canExecute()
用法:
file.canExecute();
參數(shù):該函數(shù)不接受任何參數(shù)。
返回值:此函數(shù)返回一個(gè)布爾值,表示是否可以執(zhí)行指定的文件。
異常:如果對(duì)文件的讀取訪問(wèn)被拒絕,則此方法將引發(fā)Security Exception
以下示例程序旨在說(shuō)明canExecute()函數(shù)的用法:
範(fàn)例1:文件“F:\\program.txt”是F:目錄中的現(xiàn)有文件,並且允許該程序執(zhí)行該文件的權(quán)限。
// 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
範(fàn)例2:文件“F:\\program1.txt”不存在,我們將嘗試檢查該文件是否可執(zhí)行。
// 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
注意:程序可能無(wú)法在在線IDE中運(yùn)行。請(qǐng)使用離線IDE並設(shè)置文件的路徑。
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的java file.canexecute_Java File canExecute()用法及代碼示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql数据库的字符串表示什么意思_M
- 下一篇: php5.2.10安装_安装 | 起步