这里要区分的是目录路径
如:
/opt/deve/tomcat/bin
c:\deve\tomcat\bin
都是绝对目录路径
bin
bin/data
bin\data
都是相对目录路径
通过观察,发现规律
以/开始 或者 包含\或//的都是绝对路径 或者
以/开始 或者 包含:的都是绝对路径
反之就是相对路径
介绍几个方法:
startsWith public class Stringutil { public static void main(String[] args) { String path = "/opt/bin"; System.out.println(path.startsWith("/")); } } |
结果:true
indexOf
最终结果:
/** * 传入路径,返回是否是绝对路径,是绝对路径返回true,反之 * * @param path * @return * @since 2015年4月21日 */ public boolean isAbsolutePath(String path) { if (path.startsWith("/") || path.indexOf(":") > 0) { return true; } return false; } |