这两个getResource()是使用当前ClassLoader加载资源(即资源在 Class path中),这样资源和class直接打在jar包中,避免文件路径问题.
两者不同是Class的getResource()方法是从当前.class文件路径查找资源,ClassLoader则是从jar包根目录查找.
注意:针对WEB的javaEE项目和javaSE项目,路径也是有所不同的。
下面是代码的例子,注意"/"的使用
package com.chinaso.phl;
/**
* @author piaohailin
* @date 2013-12-16
*/
public class Main {
public static void main(String[] args) {
System.out.println(Main.class.getResource("a.txt"));
System.out.println(Main.class.getResource("/a.txt"));
System.out.println(Main.class.getResource("/"));
System.out.println(Main.class.getResource("b.txt"));
// need to add resource/b.txt to build path! it will be package in jar file
System.out.println(Main.class.getClassLoader().getResource("b.txt"));
System.out.println(Main.class.getClassLoader().getResource("/"));
System.out.println(Thread.currentThread().getContextClassLoader().getResource("b.txt"));
}
}
eclipse中的输出结果
file:/I:/project/eclipse3.6/java.getResource/bin/com/chinaso/phl/a.txt
null
file:/I:/project/eclipse3.6/java.getResource/bin/
null
file:/I:/project/eclipse3.6/java.getResource/bin/b.txt
null
file:/I:/project/eclipse3.6/java.getResource/bin/b.txt
打包以后输出的结果
C:\my>java -classpath my.jar;b.txt com.chinaso.phl.Main
jar:file:/C:/my/my.jar!/com/chinaso/phl/a.txt
null
null
null
jar:file:/C:/my/my.jar!/b.txt
null
jar:file:/C:/my/my.jar!/b.txt
参考文章
http://www.diyhi.net/bbs/thread-436-1-1.html

- 大小: 8.9 KB
分享到:
相关推荐
URLTest.class.getClassLoader().getResource().getPath(); Thread.currentThread().getContextClassLoader().getResource().getPath();等多种相似方式获得的路径,不能被FileReader()和FileWriter()直接应用...
4. `FileTest.class.getClassLoader().getResource("")`:再次,这个方法也用于获取当前ClassPath的绝对URI路径,路径结果和之前的一致。 5. `ClassLoader.getSystemResource("")`:这个方法返回的是系统类加载器的...
- `getClassLoader().getResource("/")`会返回`classpath`的根目录,但`getClassLoader().getResource("/")`是错误的用法,因为这会导致路径解析出错。 #### 七、结论 通过本文的介绍,我们可以了解到Java中获取...
在 Java 中,我们可以使用 `类名.class.getClassLoader().getResource("").getPath()` 来获取编译后 class 文件目录的绝对路径。 3. JSP 编程中获得当前实际运用的相对路径和绝对路径根列表所对应的绝对路径 在 ...
System.out.println(Test.class.getClassLoader().getResource("")); ``` 4. **使用`ClassLoader.getSystemResource("")`**: - 获取系统类加载器的资源路径。 - 示例代码: ```java System.out.println...
1. 使用`Class.getResource`或`ClassLoader.getResource`,这两个方法都需要传入相对类路径的字符串。 2. 使用Guava的`Resources.getResource`,它提供了一种更简洁的接口,同时支持绝对和相对路径。 理解并掌握...
与 `Class.getResource` 不同,`ClassLoader.getResource` 会搜索整个类路径,包括标准库和用户自定义的类路径。 ### 注意事项 - 当使用 `Class.getResource` 或 `ClassLoader.getResource` 时,如果资源不在类...
该方法使用类名.class.getClassLoader().getResource()方法来获取.properties文件的URL,然后使用openConnection()方法获取连接,最后使用getInputStream()方法获取输入流。例如,Test2.class.getClassLoader().get...
一种方法是利用Java的类加载机制,通过`Class`对象的`getClassLoader().getResource("")`方法获取`ClassPath`,然后进一步处理以得到WebRoot。但这种方法只适用于类位于`WebRoot/WEB-INF/classes`下,如果类被打包到...
URL url = Configuration.class.getClassLoader().getResource(filename); String str = url.getFile(); // 转换编码 str = URLDecoder.decode(str, "utf-8"); File conf = new File(str); ...
- `DebitNoteAction.class.getClassLoader().getResource("")`和`ClassLoader.getSystemResource("")`都是获取当前类路径的根目录。 5. **使用系统属性获取路径**: - `System.getProperty("user.dir")`:获取...
Digester digester = DigesterLoader.createDigester(TestDigester.class.getClassLoader().getResource(Constants.CONFIG_MAPPINGRULE_PATH)); digester.setValidating(false);
接着,通过`URL`对象定位到"application.yml"文件,这里使用了`Test02.class.getClassLoader().getResource()`方法,它可以从类路径中找到资源文件。 ```java Yaml yaml = new Yaml(); URL url = Test02.class....
URL xmlPath = this.getClass().getClassLoader().getResource("selected.txt"); System.out.println(xmlPath); // 输出:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected....
4. **使用 `FileTest.class.getClassLoader().getResource("")`**:这种方法也可以获取当前classpath的绝对路径。 综上所述,通过理解和应用上述推荐的最佳实践及获取路径的方法,可以有效地解决Java路径问题,确保...
String f = Hello.class.getClassLoader().getResource("4.txt").getPath(); ``` - 需要注意的是,这种方式同样只能获取到类路径中的资源。 #### 注意事项 - **使用相对路径时**,务必确保路径的正确性,否则...
System.out.println(FileTest.class.getClassLoader().getResource("")); System.out.println(ClassLoader.getSystemResource("")); System.out.println(FileTest.class.getResource("")); System.out.println...