类名.class.getResource("");
java.lang.Class.getResource() 查找给定名字的资源
import java.net.URL;import java.lang.*;public class ClassDemo {
public static void main(String[] args) throws Exception {
ClassDemo c = new ClassDemo();
Class cls = c.getClass();
// finds resource relative to the class location
URL url = cls.getResource("file.txt");
System.out.println("Value = " + url);
// finds resource relative to the class location
url = cls.getResource("newfolder/a.txt");
System.out.println("Value = " + url);
}}
结果:
Value = file:/C:/Program%20Files/Java/jdk1.6.0_06/bin/file.txt
Value = null
本回答被网友采纳