关于获取放在src文件夹下读取xml文件的总结

方法一:

 通过getSession方法获取项目的位置:

String s = req.getSession().getServletContext().getRealPath("\\WEB-INF\\classes\\controller.xml");

String s = req.getSession().getServletContext().getRealPath("\\WEB-INF\\classes\\controller.xml");
		 File file = new File(s);
		 FileInputStream in = new FileInputStream(file);
		 byte [] content = new byte[1024];
		 int length = in.read(content);
		 String xml = new String(content,0,length);

因为获取到的是真实路径所以还要通过File等类的处理来或的FileInputstream

方法二:

通过获取class字节码文件getClassLoader来实现

InputStream in = aaacc.class.getClassLoader().getResourceAsStream("controller.xml");
因为获取的是InputStream所以要直接根据InputStream进行处理

ufferedReader reader = new BufferedReader(new InputStreamReader(in));
		 StringBuilder sb = new StringBuilder();      		      
		 String line = null;      
		 while ((line = reader.readLine()) != null) {      
		                  sb.append(line + "\n");      
		 }
		 String xml = sb.toString();






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值