现在的位置: 首页 > 综合 > 正文

关于java 读取propterties 文件的疑惑 和问题的解决[转载]

2013年08月05日 ⁄ 综合 ⁄ 共 4804字 ⁄ 字号 评论关闭

转载: http://www.blogjava.net/gml520/archive/2008/10/03/232245.html 

 

   最近在重构自己的小Demo 想把自己的下Demo 变成可以配置的程序。但是遇到了一些困难,走了不少的弯路,最终还是解决了。 如果你是个牛人,你不用看了,写这些东西是给和我一样的菜鸟看的。
先说说我的Demo吧,我写的是一个简单新闻管理系统,很简陋的那种,不过麻雀虽小,但是五脏俱全。
基本的增、删、查、改,都有,用到了FCKeditor。一开始我连接数据库用的是最原始的方法,把数据库的连接信息全写在代码里,写完以后就没去管他了,十一放假这几天,看了看自己的代码,觉得代码结果不好,于是就开始重构自己的代码,首先就是重写数据库的连接管理我把连接数据库的一些基本信息写到了database.properties 文件里,然后从程序中读取连接数据库所需要的信息。但是看似简单的东西,比且很容易的完成的工作,我却遇到了不少麻烦。首先就是路径的问题,其次是该以何种方式读取;就这两个问题困扰了两天。(呵呵,见笑了

java读取propterties文件有三种方法:

  ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
  Class.getResourceAsStream (
"/some/pkg/resource.properties");
  ResourceBundle.getBundle (
"some.pkg.resource");

注意括号中代码的不同。

    先让大家看一段代码吧:
DataSource.java 文件:

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *一个单子设计模式数据库连接
 * 
@author guanminglin
 
*/
public class DataSource {

    public static Connection conn;

    public DataSource() throws IOException {
        {
            
try {

                Properties props = new Properties();
                
//一定要用DataSource 
                InputStream in = DataSource.class.getResourceAsStream("/org/news/comm/database.properties");

                //原先试过这下面两种方法,但是都不行
                
//FileInputStream in = new FileInputStream("database.properties");
                //InputStream in = Object.class.getResourceAsStream("/org/news/comm/database.properties")

                //下面两行是用来测试的
                String s 
= in.toString();
                System.out.println(
"s=="+s);
                
                props.load(in);
                in.close();
                String drivers 
= props.getProperty("jdbc.drivers");
                String url 
= props.getProperty("jdbc.url");
                String username 
= props.getProperty("jdbc.username");
                String password 
= props.getProperty("jdbc.password");
                Class.forName(drivers);
                conn 
= DriverManager.getConnection(url, username, password);
                in.close();
            } 
catch (SQLException ex) {
                Logger.getLogger(DataSource.
class.getName()).log(Level.SEVERE, null, ex);
            } 
catch (ClassNotFoundException ex) {
                Logger.getLogger(DataSource.
class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }
   

    /**
     *如果连接为空则建立一个新连接
     * 
@return
     
*/
    
public static Connection getConnection() throws IOException {
        
if (conn == null) {
            
new DataSource();
        }
        
return conn;
    }
    
public void test(){
        String str 
= new String();
        

    }

    public static void main(String[] args) throws IOException {
        
// TODO code application logic here
        //用于测试
        new DataSource();
        System.out.print(conn);
    }
}

我的文件包结构:

第一个要说的是:FileInputStream in = new FileInputStream("database.properties");
网上有很多人说这条语句是从当前目录中加载database.properties文件的,但是我得出的结果是:它是从classpath 中加载database.properties文件的
因为我用的是IDE 是NetBeans 我以为直接放在当前包内就可以了,但是我发现这样不行。具体放哪里我也不知道,后来用了另一种方法:

InputStream in = Object.class.getResourceAsStream("/org/news/comm/database.properties")
基本上是可以用了,一开始测试的时候还挺开心的。很顺利的就通过了,但是当我真正运行项目的时候问题又来了,程序报了 空指针引用 错误。我就很纳闷了为什么在单个文件里测试可以很容易的通过,一运行项目就会报错呢?于是开始Debug,不Debug 不知道,一用吓一跳:
InputStream in = Object.class.getResourceAsStream("/org/news/comm/database.properties")

in的值竟然是空的,难怪会报空指针错误了。单独测试DataSource.java文件的时候运行正常啊,没有报错,in 也有值,但是为什么运行项目的时候又没有值了呢?很是不解。

于是我google 了一把,最后看现了几篇好文章 说的是如何读取 propterties 文件。最终我发现用第二种
方法是有弊端的,关键就在Object 上,它所有类的父类,在单个文件中用他来读取propterties文件还可以但是一运行项目,他就不起作用了,也不知道为什么。难道是java 的bug??或者说是NetBeans的bug??
我同样的Eclipse 下测试过,结果是一样的。

最后我将输入流语句稍微改了一下,变成下面的样子:
InputStream in = DataSource.class.getResourceAsStream("/org/news/comm/database.properties")

其实就是改个类名而已,让他自身去定位propterties文件,就解决问题了。(注意括号中红色的部分,org前面的 "/" 绝对不能少,要不然会报错的。
无论是测试单文件还是,测试整个项目都没问题了。(那个高兴啊

database.propterties 文件很简单:

jdbc.drivers = com.mysql.jdbc.Driver
jdbc.url 
= jdbc:mysql://localhost:3306/news
jdbc.username = root
jdbc.password 
= root

总结一下读取propterties文件方法的不同:(JavaWorld里的内容,呵呵拿来用一下)

Behavioral differences

Method Parameter format Lookup failure behavior Usage example
ClassLoader.
getResourceAsStream()
"/"-separated names; no leading "/" (all names are absolute) Silent (returns null) this.getClass().getClassLoader()
.getResourceAsStream
("some/pkg/resource.properties")
Class.
getResourceAsStream()
"/"-separated names; leading "/" indicates absolute names; all other names are relative to the class's package Silent (returns null) this.getClass()
.getResourceAsStream
("resource.properties")
ResourceBundle.
getBundle()
"."-separated names; all names are absolute; .properties suffix is implied Throws unchecked
java.util.MissingResourceException
ResourceBundle.getBundle
("some.pkg.resource")

推荐给大家一篇JavaWorld里面好的文章,里面讲述使用不同方式读出propterties文件的方式,以及他们之间的不同:Smartly load your properties

抱歉!评论已关闭.