Java得到类路径的多种方法

经常使用:
(1).Test.class.getResource("")
获得的是当前类FileTest.class文件的URI目录。不包括本身!
(2).Test.class.getResource("/")
获得的是当前的classpath的绝对URI路径。
(3).Thread.currentThread().getContextClassLoader().getResource("")
获得的也是当前ClassPath的绝对URI路径。
(4).Test.class.getClassLoader().getResource("")
获得的也是当前ClassPath的绝对URI路径。
(5).ClassLoader.getSystemResource("")
获得的也是当前ClassPath的绝对URI路径。
尽可能不要使用相对于System.getProperty("user.dir")当前用户目录的相对路径,后面能够看出得出结果五花八门。
(6) new File("").getAbsolutePath()也可用。
spa

(7) Class类的getResourceAsStream方法,读取同一个包中的配置文件
orm

相关文章
相关标签/搜索