Java获取项目内文件路径
作者:互联网
System.out.println("class.getResource()输出^_^"); // file:/.../target/classes/.../mybatis-config.xml System.out.println(TestDemo.class.getResource("mybatis-config.xml")); // file:/.../target/classes/ System.out.println(TestDemo.class.getResource("/")); // file:/.../target/classes/../mybatis
// 当前类所在路径 System.out.println(TestDemo.class.getResource("")); System.out.println("----------------------------------"); System.out.println("getClassLoader().getResource()输出^_^"); // 找不到输出null System.out.println(TestDemo.class.getClassLoader().getResource("mybatis-config.xml")); // 找不到输出null System.out.println(TestDemo.class.getClassLoader().getResource("/")); // file:/../target/classes/ System.out.println(TestDemo.class.getClassLoader().getResource("")); System.out.println("----------------------------------"); System.out.println("其他形式调用^_^"); // file:/.../target/classes/ System.out.println(Thread.currentThread().getContextClassLoader().getResource("")); // 找不到输出null System.out.println(Thread.currentThread().getContextClassLoader().getResource("mybatis-config.xml")); // file:/.../target/classes/ System.out.println(ClassLoader.getSystemResource(""));
TestDemo.class.getResource("")
当前类所在路径
TestDemo.class.getResource("/")
编译路径根目录
class.getClassLoader().getResource("")
编译路径根目录
标签:getResource,Java,TestDemo,路径,System,获取,println,class,out 来源: https://www.cnblogs.com/yixiu868/p/12206152.html