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 系统,会调用本地命令<br>
	 * hide:attrib ".mqtt_client.properties" +H<br>
	 * 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 系统,会调用本地命令<br>
	 * hide:attrib ".mqtt_client.properties" +H<br>
	 * 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见附件

本文出自 “whuang” 博客,请务必保留此出处http://huangkunlun520.blog.51cto.com/2562772/1599339

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。