Java web 项目读取src或者tomcat下class文件夹下的xml文件或者properties文件

Java web 项目读取src或者tomcat下class文件夹下的xml文件或者properties文件
//生成一个文件对象:
File file = new File(getClass().getClassLoader().getResource(“test.xml”).getPath());
//直接得到一个输入流:
InputStream in = getClass().getClassLoader().getResourceAsStream(“test.xml”);

	//在当前线程获取--这种方法不大稳定
	//String path = Thread.currentThread().getContextClassLoader().getResource("").getPath()+"/test.xml";
	
            //推荐使用此方法
	String path = getClass().getClassLoader().getResource("test.xml").getPath();

猜你喜欢

转载自blog.csdn.net/weixin_43217493/article/details/88717064
今日推荐