Java File类boolean setExecutable(boolean exec_file)方法,包含示例

文件类boolean setExecutable(boolean exec_file) (File Class boolean setExecutable(boolean exec_file))

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

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

  • This method is used to set the access permission to owner’s or everybody's to execute the file.

    此方法用于将所有者或所有人的访问权限设置为执行文件。

  • The return type of this method is boolean so the expected value will be true or false if returns true that means file is executable else file is not executable.

    此方法的返回类型为boolean,因此如果返回true表示文件是可执行的,则预期值为true或false,否则文件不可执行。

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

    如果未提供文件写入权限,则此方法可能引发异常(即Security Exception)。

  • This method is overridable and first overridable method accepts one parameter and other accepts two-parameter.

    此方法是可重写的,第一个可重写的方法接受一个参数,而其他方法接受两个参数。

Syntax:

句法:

    boolean setExecutable(Boolean exec_file){
    }

Parameter(s):

参数:

We pass only one object as a parameter in the method of the File i.e exec_file(Executable file ).

在File方法中,我们仅传递一个对象作为参数,即exec_file(Executable file)。

Return value:

返回值:

The return type of this method is Boolean if it returns true so the file is executable and else returns false file is not executable.

如果此方法返回true,则该方法的返回类型为Boolean ,因此该文件是可执行的,否则返回false不可执行的文件。

Java程序演示setExecutable()方法的示例 (Java program to demonstrate example of setExecutable() 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 ExecutableFileClass {
    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 setExecutable(true) is allowed to execute 
            // the file because we passed true as a parameter in the method.
            if (file1.setExecutable(true))
                System.out.println("This file " + file1.getName() + " " + "is executable  because access permission is given");
            else
                System.out.println("This file " + file1.getName() + " " + "is not executable because access permission is not given");

            // By using setExecutable(false) is not allowed to execute 
            // the file because we passed false as a parameter 
            //in the method of the file.
            if (file2.setExecutable(false))
                System.out.println("This file " + file2.getName() + " " + "is executable because access permission is given");
            else
                System.out.println("This file " + file2.getName() + " " + "is not executable because access permission is not given");
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac ExecutableFileClass.java

D:\Programs>java ExecutableFileClass
This file myjava.txt is executable because access permission is given
This file java.txt is not executable because access permission is not given


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值