fileset java_Java File setExecutable()用法及代码示例

setExecutable()方法是File类的一部分。该函数设置所有者或所有人执行抽象路径名的权限。

该函数是重载函数一个函数需要两个参数,而另一个仅需要一个。

函数签名:

public boolean setExecutable(boolean a, boolean b)

public boolean setExecutable(boolean a)

函数语法:

file.setExecutable(boolean a, boolean b)

file.setExecutable(boolean a)

参数:该函数是重载函数:

对于第一次过载:

如果将真值作为第一个参数传递,则允许它执行抽象路径名,否则不允许执行该文件。

如果将true值作为第二个参数传递,则执行权限仅适用于所有者,否则适用于所有人

对于第二次过载:

如果将真实值作为参数传递,则允许它执行抽象路径名,否则不允许执行该文件。

返回值:无论操作是否成功,此函数都会返回布尔值。

异常:如果不允许该函数对该文件进行写访问,则此方法将引发Security Exception。

下面的程序将说明setExecutable()函数的用法

范例1:我们将尝试更改f:目录中现有文件的所有者的setExecutable权限。

// Java program to demonstrate the

// use of setExecutable() function

import java.io.*;

public class solution {

public static void main(String args[])

{

// try-catch block to handle exceptions

try {

// Create a file object

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

// Check if the Executable permission

// can be set to new value

if (f.setExecutable(true)) {

// Display that the Executable permission

// is be set to new value

System.out.println("Executable permission"

+ " is set");

}

else {

// Display that the Executable permission

// cannot be set to new value

System.out.println("Executable permission"

+ " cannot be set");

}

}

catch (Exception e) {

System.err.println(e.getMessage());

}

}

}

输出:

Executable permission is set

范例2:我们将尝试更改f:目录中每个现有文件的setExecutable权限。

// Java program to demonstrate the

// use of setExecutable() function

import java.io.*;

public class solution {

public static void main(String args[])

{

// try-catch block to handle exceptions

try {

// Create a file object

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

// Check if the Executable permission

// can be set to new value

if (f.setExecutable(true, false)) {

// Display that the Executable permission

// is be set to new value

System.out.println("Executable permission"

+ " is set");

}

else {

// Display that the Executable permission

// cannot be set to new value

System.out.println("Executable permission"

+ " cannot be set");

}

}

catch (Exception e) {

System.err.println(e.getMessage());

}

}

}

输出:

Executable permission is set

这些程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的父文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值