操作配置文件.properties

我打算写一篇公共通用的操作配置文件,但是遇到的问题,网上找了很多,都遇到了问题。

 

-------- Java代码package fileOperate;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadProperties {
private String path ;
public ReadProperties(){
Properties config = new Properties();
InputStream is = null;
is = this.getClass().getClassLoader().getResourceAsStream("/ReadProperties.properties");
//is = this.getClass().getClassLoader().getSystemResourceAsStream("ReadProperties.properties");
try {
config.load(is);
path = config.getProperty("path");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(null!=is)
is.close();
if(null!=config)
config.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public String getPath() {
return path;
}

public static void main(String[] args){
ReadProperties properties = new ReadProperties();
System.out.println(properties.getPath());
}
}


------- 问题:

网上的都说:
a. 通过 InputStream inputstream = ClassLoader.getSystemResourceAsStream("info.properties");
b. 通过 InputStream inputstream = this.getClass().getResourceAsStream("/info.properties");
但是 我  is = this.getClass().getClassLoader().getResourceAsStream("/ReadProperties.properties");  加了‘/’反而报错,不加则正常。
谁不能帮我解决下....


报错信息:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load(Unknown Source)
at fileOperate.ReadProperties.<init>(ReadProperties.java:15)
at fileOperate.ReadProperties.main(ReadProperties.java:36)

 

已解决: 

出错的原因,没有部署到tocmat服务里面,然后我把这段代码写进servlet里面,然后加‘/’就能跑通, 一开始一直用Main方法测试,总是出错,现在用httpservlet 访问就没错,可能是加‘/’访问的是tocmat下面的classes的配置文件!

----- 代码如下:
package fileOperate;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReadPropertiesServlet extends HttpServlet{

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Properties config = new Properties();
InputStream is = null;
//is = this.getClass().getClassLoader().getSystemResourceAsStream("ReadProperties.properties");
try {
is = this.getClass().getClassLoader().getResourceAsStream("/ReadProperties.properties");
if(is == null)
{
System.out.println("/ll");
return;
}
config.load(is);
  String path = config.getProperty("path"); // 这样path就能读到 
System.out.println(path);
} catch (Exception e) {
System.out.print("aaa");
e.printStackTrace();
} finally {
try {
if(null!=is)
is.close();
if(null!=config)
config.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

----- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>ReadPropertiesServlet</servlet-name>
<servlet-class>fileOperate.ReadPropertiesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ReadPropertiesServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

--------配置文件
path=D\:/Update/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值