properties文件的解析

此文章是从网上看到一篇实用小文章,感觉不过,摘录下来的!如有问题,可及时联系,可立刻做相应处理!

Java读取.properties

配置文件的几种方法

在做java工程时,

经常会将一些配置信息放到

.properties

文件中,

需要用时再去对应的文件中

读取,下面提供几种常用的读取

.properties

文件的方法,并列举这些方法的特点。

一、读文件方式读取

.properties

文件。

关键代码如下:

    try{ 

// 创建Properties对象

 

Properties p = new Properties(); 

// 设置读取文件路径

String s_xmlpath="config.properties"; 

 

InputStream io=Test.class.getClassLoader().getResourceAsStream(s_xmlpath); 

 

// 加载文件

 

p.oad(io); 

 

// 取得文件的值

 

system.out.println(p.getProperty("key")); 

// 关闭流

io.close();  

}catch(Exception ex){ 

ex.printStackTrace(); 

该方法可放到

Servlet

中,在工程初期化时一次性加载配置文件,具体代码如下:

import java.io.IOException; 

import java.io.InputStream; 

import java.io.PrintWriter; 

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 Test extends HttpServlet { 

public static Properties p; 

private static Test instance = null; 

/** 

* Constructor of the object. 

* 默认构造方法

*/ 

public Test() { 

super(); 

/** 

* Constructor of the object. 

* 私有构造方法,调用

init()

方法读取配置文件

*/ 

private Test(String str) { 

super(); 

try { 

this.init(); 

} catch (ServletException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

/** 

* 静态方法,取得对象实例

* @return 

*/ 

public static Test getInstance() 

// 当前实例为空时

if(instance == null) 

instance = new Test(""); 

return instance; 

/** 

* Destruction of the servlet. <br> 

*/ 

public void destroy() { 

super.destroy(); // Just puts "destroy" string in log 

// Put your code here 

/** 

* The doGet method of the servlet. <br> 

* This method is called when a form has its tag value method equals to get. 

* @param request the request send by the client to the server 

* @param response the response send by the server to the client 

* @throws ServletException if an error occurred 

* @throws IOException if an error occurred 

*/ 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

……

/** 

* The doPost method of the servlet. <br> 

* This method is called when a form has its tag value method equals to post. 

* @param request the request send by the client to the server 

* @param response the response send by the server to the client 

* @throws ServletException if an error occurred 

* @throws IOException if an error occurred 

*/ 

public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

……

/** 

* Initialization of the servlet. <br> 

* @throws ServletException if an error occurs 

*/ 

public void init() throws ServletException { 

try{ 

// 创建Properties对象

p = new Properties(); 

// 设置读取文件路径

String s_xmlpath="sql.properties"; 

InputStream io=Test.class.getClassLoader().getResourceAsStream(s_xmlpath); 

//调用里面的信息

}

posted @ 2016-03-18 10:01  ~铁臂阿童木~  阅读(448)  评论(0编辑  收藏  举报