java修改文件只读_Java windows中设置文件只读

windows中如何设置文件只读或隐藏呢?

(1)windows设置文件只读/***

* 设置为只读

* @param filePath

* @return

*/

public static int readOnly(String filePath){

if(new File(filePath).exists()){

Process p=CMDUtil.executeCmd("attrib "+filePath+" +R");

try {

p.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

return SystemHWUtil.NEGATIVE_ONE;

}

return p.exitValue();

}else{

return SystemHWUtil.NEGATIVE_ONE;

}

}

(2)去掉文件只读属性/***

* 设置为可写

* @param filePath

* @return

*/

public static int removeReadOnly(String filePath){

if(new File(filePath).exists()){

Process p=CMDUtil.executeCmd("attrib "+filePath+" -R");

try {

p.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

return SystemHWUtil.NEGATIVE_ONE;

}

return p.exitValue();

}else{

return SystemHWUtil.NEGATIVE_ONE;

}

}

(3)设置文件隐藏/***

* 仅适用于windows 系统,会调用本地命令

* hide:attrib ".mqtt_client.properties" +H

* show:attrib ".mqtt_client.properties" -H

* @param filePath

* @return

*/

public static int hide(String filePath){

if(new File(filePath).exists()){

Process p=CMDUtil.executeCmd("attrib "+filePath+" +H");

try {

p.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

return SystemHWUtil.NEGATIVE_ONE;

}

return p.exitValue();

}else{

return SystemHWUtil.NEGATIVE_ONE;

}

}

(4)去掉文件隐藏属性/***

* 仅适用于windows 系统,会调用本地命令

* hide:attrib ".mqtt_client.properties" +H

* show:attrib ".mqtt_client.properties" -H

* @param filePath

* @return

*/

public static int show(String filePath){

if(new File(filePath).exists()){

Process p=CMDUtil.executeCmd("attrib "+filePath+" -H");

try {

p.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

return SystemHWUtil.NEGATIVE_ONE;

}

return p.exitValue();

}else{

return SystemHWUtil.NEGATIVE_ONE;

}

}

(1)(2)(3)(4)依赖的方法:public static Process executeCmd(String command)

{

Process p = null;

try

{

p = Runtime.getRuntime().exec(CMD_SHORT + command);

}

catch (IOException e)

{

e.printStackTrace();

}

return p;

}

说明:

CMD_SHORT 的值是:"cmd /c "SystemHWUtil.NEGATIVE_ONE的值是-1

(5)判断文件或目录subFileStr 是否存在于parentFolderStr(目录)中/***

* 判断父目录parentFolderStr 是否有文件subFileStr(也可以是目录)

* @param parentFolderStr

* @param subFileStr

* @return : 返回null,说明不存在

*/

public static File subFileExist(String parentFolderStr,String subFileStr)

{

if(!parentFolderStr.endsWith(File.separator)){

parentFolderStr+=File.separator;

}

File subFolder=new File(parentFolderStr+subFileStr);

if(subFolder.exists()){

return subFolder;

}else{

return null;

}

}

说明:如果存在则返回子目录绝对路径;如果不存在,则返回null.

依赖的jar见附件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值