关于相对路径

时间:2022-11-27 13:48:54
public class FileTest {
public static void main(String[] args){
System.out.println(FileTest.class.getClassLoader().getResource("1.txt").getPath());
System.out.println(FileTest.class.getClassLoader().getResource("").getPath());
System.out.println(FileTest.class.getClassLoader().getResource("tools/1.txt").getPath());
}

}
结果输出如下:/C:/Users/Jane/workspace/StudyProject/bin/1.txt/C:/Users/Jane/workspace/StudyProject/bin/Exception in thread "main" java.lang.NullPointerExceptionat com.example.test.FileTest.main(FileTest.java:9)
1.txt在工程的bin目录下存在时,运行不会报空指针异常。
tools/1.txt是在工程下(非bin目录)新建的文件夹文件</p></div>