以下是常用的java路径获取方法:
String userDir = System.getProperty("user.dir");
System.out.println("user.dir :" + userDir);
String userJome = System.getProperty("user.home");
System.out.println("user.home :" + userJome);
String javaClassPath = System.getProperty("java.class.path");
System.out.println("java.class.path:" + javaClassPath);
String canonicalPath = new File("").getCanonicalPath();
System.out.println("canonicalPath :" + canonicalPath);// 获取标准路径
String absolutePath = new File("").getAbsolutePath();
System.out.println("absolutePath :" + absolutePath);// 获取绝对路径
String currentClassPath = this.getClass().getResource("").toURI().getPath();
System.out.println("currentClassPath:" + currentClassPath);// 获取当前类路径
String LoadClassPath = this.getClass().getClassLoader().getResource("").toURI().getPath();
System.out.println("LoadClassPath :" + LoadClassPath);// 获取加载类路径
以下是java web项目路径获取方法(toURI解决了空格问题;由于编写完成代码之后我们要进行单元测试,所以projectPath要有两种获取方式):
String projectPath;
try {
projectPath = this.getClass().getClassLoader().getResource("").toURI().getPath() + "users.xml";
} catch (NullPointerException e) {
projectPath = System.getProperty("user.dir") + "\\users.xml";
}
System.out.println("projectPath:" + projectPath);// 获取项目路径
以下是他人收集的非常好的网址:
Java路径问题解决方案收集:http://www.cnblogs.com/rongxh7/archive/2010/04/22/1718178.html
Java获取当前路径:http://www.cnblogs.com/diyunpeng/archive/2011/06/06/2073567.html
java(Web)中相对路径,绝对路径问题总结:http://www.blogjava.net/meil/archive/2006/10/10/73908.html
粗浅理解,还望同学们指正。
欢迎阅读相关文章: http://hi.csdn.net/linchengzhi