fileset java_Java File setWritable()方法

Java File setWritable()方法

java.io.File.setWritable(boolean writable, boolean ownerOnly) 方法设置所有者或在此抽象路径名所有人的写权限。

1 语法

public boolean setWritable(boolean writable, boolean ownerOnly)

2 参数

writable:如果为true,允许写访问权限;如果为false,写访问权限是不允许的。

ownerOnly:如果为true,则写访问权限仅适用于所有者,否则它适用于所有人。

3 返回值

当且仅当操作成功时该方法返回true。

4 示例

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* java.io.File.setWritable(boolean writable, boolean ownerOnly)方法的例子

*/

import java.io.File;

public class Demo {

public static void main(String[] args) {

File f = null;

boolean bool = false;

try {

// create new File object

f = new File("d:/test.txt");

// returns true if file exists

bool = f.exists();

// if file exists

if(bool) {

// set file access to writable for everybody

bool = f.setWritable(true, false);

// print

System.out.println("setWritable() succeeded?: "+bool);

// checks whether the file is writable

bool = f.canWrite();

// prints

System.out.print("Is file writable?: "+bool);

}

} catch(Exception e) {

// if any error occurs

e.printStackTrace();

}

}

}

输出结果为:

setWritable() succeeded?: true

Is file writable?: true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值