方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。
因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
具体举例如下:
//ServletContext.getRealPath(name)读取路径
privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)
throwsServletException,IOException {
//response.setContentType("text/html;charset=utf-8");
String path = "/WEB-INF/jdbc_connection.properties"; //读取WEB-INF中的配置文件
String realPath = getServletContext().getRealPath(path);//getServletContext()相当于http://localhost/demo05
//所以后面的path只需要以应用demo/开头具体的部署目录路径即可,如上面的/web-in…
System.out.println(realPath);
InputStreamReader reader =new InputStreamReader(newFileInputStream(realPath),"utf-8");
Properties props = new Properties();
props.load(reader); //load个人建议还是用Reader来读,因为reader体系中有个InputStreamReader可以指定编码
String jdbcConValue = props.getProperty("jdbc_con");
System.out.println(jdbcConValue);
System.out.println("加载src包下的资源------------------------");
path = "/WEB-INF/classes/com/test/servlet/jdbc_connection.properties"; //读取WEB-INF中的配置文件
realPath=getServletContext().getRealPath(path);
System.out.println(realPath);
reader = new InputStreamReader(new FileInputStream(realPath),"utf-8");
props.load(reader); //load个人建议还是用Reader来读,因为reader体系中有个InputStreamReader可以指定编码
jdbcConValue = props.getProperty("jdbc_con");
System.out.println("second::"+jdbcConValue);
}
方式二:采用ResourceBundle类读取配置信息,
优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。
缺点:只能加载类classes下面的资源文件且只能读取.properties文件。
-
-
-
-
-
-
-
-
-
-
- public static List getAllMessage(String propertyName) {
-
- ResourceBundle rb = ResourceBundle.getBundle(propertyName.trim());
-
- Enumeration allKey = rb.getKeys();
-
- List valList = new ArrayList();
- while (allKey.hasMoreElements()) {
- String key = allKey.nextElement();
- String value = (String) rb.getString(key);
- valList.add(value);
- }
- return valList;
- }
方式三:采用ClassLoader方式进行读取配置信息
优点是:可以在非Web应用中读取配置资源信息,可以读取任意的资源文件信息
缺点:只能加载类classes下面的资源文件。
-
-
-
-
-
- private static void use_classLoador(){
-
- InputStream is=TestJava.class.getClassLoader().getResourceAsStream("message.properties");
-
- String filePath=TestJava.class.getClassLoader().getResource("message.properties").getFile();
- System.out.println(filePath);
-
-
-
- BufferedReader br= new BufferedReader(new InputStreamReader(is));
- Properties props = new Properties();
-
- try {
- props.load(br);
- for (Object s : props.keySet())
- System.out.println(s);
- } catch (IOException e) { e.printStackTrace();}
- }
方法4 getResouceAsStream
XmlParserHandler.class.getResourceAsStream 与classloader不同
使用的是当前类的相对路径
- BufferedReader br=new BufferedReader(
- new InputStreamReader(XmlParserHandler.class.
- getResourceAsStream("./rain.xml"), "GB2312"));
- InputSource is=new InputSource(br);
方法5 PropertiesLoaderUtils工具类
-
-
-
-
- private static void springUtil(){
- Properties props = new Properties();
- while(true){
- try {
- props=PropertiesLoaderUtils.loadAllProperties("message.properties");
- for(Object key:props.keySet()){
- System.out.print(key+":");
- System.out.println(props.get(key));
- }
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
-
- try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}
- }
- }
修改Properties
-
-
-
-
-
-
-
-
- public static void updateProperties(String fileName,Map keyValueMap) {
-
-
- String filePath = PropertiesUtil.class.getClassLoader().getResource(fileName).getFile();
- Properties props = null;
- BufferedWriter bw = null;
-
- try {
- filePath = URLDecoder.decode(filePath,"utf-8");
- log.debug("updateProperties propertiesPath:" + filePath);
- props = PropertiesLoaderUtils.loadProperties(new ClassPathResource(fileName));
- log.debug("updateProperties old:"+props);
-
-
- bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
-
- props.clear();
-
- for (String key : keyValueMap.keySet())
- props.setProperty(key, keyValueMap.get(key));
-
- log.debug("updateProperties new:"+props);
- props.store(bw, "");
- } catch (IOException e) {
- log.error(e.getMessage());
- } finally {
- try {
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
1. java 读取/WEB-INF/classes下的配置文件
private void getConfigMessage(){
//获取/WEB-INF/classes下的配置文件config.properties
InputStream is = getClass().getResourceAsStream("/config.properties");
Properties props = new Properties();
try {
//加载配置文件
props.load(is);
email = props.getProperty("GlobalAPI.email");
password = props.getProperty("GlobalAPI.password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.java获取WebContent根目录下的配置文件的路径
public String getPath(){
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
String path = request.getSession().getServletContext().getRealPath("");
path=path+"/xxx.xml";
return path;
}
3.java获取/WebContent/WEB-INF/目录下某配置文件的路径
String url = getClass().getResource("/xxx.xml").toString();