java设置文件属性(隐藏,只读...)

当要设置是否是可读或者是隐藏时,在java中除了提供File.setReadOnly()外,就无其他方法了

你可以在java中用Runtime.getRuntime().exec( "attrib " + "\ " " + file.getAbsolutePath() + "\ " "+ " +R ")该方法可以实现


1. 当Java.io中,如果文件的操作的时候,判断是否隐藏用File.ishiden()判断是否只读,可用File.canWrite().
2. 当要设置是否是可读或者是隐藏时,在java中除了提供File.setReadOnly()外,就无其他方法了。
所以我们必须到Dos环境下去设置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R")该方法可以实现。因为路径file.getAbsolutePath()中可能会还有空格,所以必须用引号把它括起来,当作一个参数。这样就可以实现了
(1) 设置只读Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +R");
(2) 设置可写Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -R");
(3) 设置隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " +H");
(4) 设置非隐藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath() + """+ " -H");


Filefile=newFile("D:/Aerchi.txt");
if(file.exists()){
file.setWritable(false);
System.out.println("设置只读成功");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值