首页 > 代码库 > Java路径获取
Java路径获取
package unit02; /** * * @time 2014年9月18日 下午10:29:48 * @porject ThinkingInJava * @author Kiwi */ public class Test03 { private String getPathByPoint() { return this.getClass().getClassLoader().getResource(".").getPath(); } private String getPathByNothing() { return this.getClass().getClassLoader().getResource("").getPath(); } private String getResourcePath() { return this.getClass().getResource("").getPath(); } private String getResourcePathByPoint() { return this.getClass().getResource(".").getPath(); } private String getThreadPath() { return Thread.currentThread().getContextClassLoader().getResource("").getPath(); } private String getThreadPathByPoint() { return Thread.currentThread().getContextClassLoader().getResource(".").getPath(); } public static void main(String[] args) { Test03 test03 = new Test03(); System.out.println("this.getClass().getClassLoader().getResource(\".\").getPath() = \n" + test03.getPathByPoint()); System.out.println("this.getClass().getClassLoader().getResource(\"\").getPath() = \n" + test03.getPathByNothing()); System.out.println("this.getClass().getResource(\"\").getPath() = \n" + test03.getResourcePath()); System.out.println("this.getClass().getResource(\".\").getPath() = \n" + test03.getResourcePathByPoint()); System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"\").getPath() = \n" + test03.getThreadPath()); System.out.println("Thread.currentThread().getContextClassLoader().getResource(\".\").getPath() = \n" + test03.getThreadPathByPoint()); System.out.println(System.getProperty("user.dir")); System.out.println(System.getProperty("java.class.path")); } }
运行结果:(注:测试环境:Eclipse; 项目名称:ThinkingInJava;包名称:unit02)
this.getClass().getClassLoader().getResource(".").getPath() = /F:/java/java_workspace/ThinkingInJava/bin/this.getClass().getClassLoader().getResource("").getPath() = /F:/java/java_workspace/ThinkingInJava/bin/this.getClass().getResource("").getPath() = /F:/java/java_workspace/ThinkingInJava/bin/unit02/this.getClass().getResource(".").getPath() = /F:/java/java_workspace/ThinkingInJava/bin/unit02/Thread.currentThread().getContextClassLoader().getResource("").getPath() = /F:/java/java_workspace/ThinkingInJava/bin/Thread.currentThread().getContextClassLoader().getResource(".").getPath() = /F:/java/java_workspace/ThinkingInJava/bin/F:\java\java_workspace\ThinkingInJavaF:\java\java_workspace\ThinkingInJava\bin;F:\java\java_workspace\code\mindview.jar
Java路径获取
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。