maven项目-读取resource下的文件

在maven工程中,我们会将配置文件放到src/main/resources下面。

它编译的路径直接位于classes下面,这个路径其实就是classPath的路径,所以在resource根目录下的配置文件其实就是classPath的路径

 ClassLoader classLoader = TestDom.class.getClassLoader();  
        URL resource = classLoader.getResource("test.xml");  
        String path = resource.getPath();  
        System.out.println(path); 

这样我们就可以直接拿到路径。

猜你喜欢

转载自blog.csdn.net/liuziteng0228/article/details/80721331