zoukankan      html  css  js  c++  java
  • 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); 

    //调用里面的信息

    }

  • 相关阅读:
    程灵素:我走过山的时候山不说话
    编译原理自学计划
    由一个虚构的例子谈谈中小型研发型项目的技术管理及成本控制(全文)
    用3种IDE导入Linux 2.26 内核源码
    Web风行者的设计方案与计划
    使用pyste自动生成c++类的python wrapper
    让log4cpp日志文件超过2G(Linux下)的方法
    python绑定c++程序
    网络风行者(KSpider)的规则体系结构
    检测您的浏览器是否支持 HTML5 视频方法
  • 原文地址:https://www.cnblogs.com/atongmyuxiaowanzi/p/5290935.html
Copyright © 2011-2022 走看看