/*
* 检查文件是否有对应权限
* @return 如果有权限返回null,否则返回没有权限的字符串说明
* chk "/类型[f|d] +权限[r|w|x] -允许属性[h|r]"
*/
private String checkFile (String absolutePath, String chk) {
File f = new File(absolutePath);
if (!f.exists()) {
return "[" +absolutePath+ "]" + getText(R.string.pathNotExist);
}
chk = chk.toLowerCase(Locale.US);
if ( (chk.indexOf("+r") > -1) && !f.canRead()) {//需要检测可读,却无
return "[" +absolutePath+ "]" + getText(R.string.pathCantRead);
}
if ( (chk.indexOf("+w") > -1) && !f.canWrite()) {
return "[" +absolutePath+ "]" + getText(R.string.pathCantWrite);
}
if ( (chk.indexOf("/f") > -1) && !f.isFile()) {
return "[" +absolutePath+ "]" + getText(R.string.pathNotFile);
} else if ( (chk.indexOf("/d") > -1) && !f.isDirectory()) {
return "[" +absolutePath+ "]" + getText(R.string.pathNotDir);
}
if ( (chk.indexOf("-h") > -1) && f.isHidden()) {
return "[" +absolutePath+ "]" + getText(R.string.pathIsHide);
}
if ( (chk.indexOf("-r") > -1) && !f.isAbsolute()) {
return "[" +absolutePath+ "]" + getText(R.string.pathIsRelative);
}
return null;//完全正常
}
用法
String tip = checkFile(dirPath, "+r/d");
if (null != tip) {
flowTip(tip);
return;
}