Posted on 2009-06-07 01:02
Gavin.lee 閱讀(1303)
評論(0) 編輯 收藏 所屬分類:
java SE & EE
我們在項目中肯定會碰到很多配置文件情況,在這種情況下,加載屬性文件就是不可避免的了。
我這里提供兩個方案:
【一】直接在src目錄下或在其子包下:
package com.Gavin.tools.des;

import java.util.ResourceBundle;

/** *//**
* @descripte load resource
* @author Gavin.lee
* @date 2009-5-19上午09:49:32
* @version 1.0
*
*/

public class ResourceLoader
{
private ResourceBundle resBundle;

public ResourceLoader(String resourceName)
{
resBundle = ResourceBundle.getBundle(resourceName);
}

public String getString(String key)
{
return resBundle.getString(key);
}

public static void main(String args[])
{
System.out.println(new ResourceLoader("config").getString("username")); //config in src
System.out.println(new ResourceLoader("doingjava.config").getString("username")); //config in doingjava
}
}

使用 Properties類中的load方法


package com.yixun.wap;


/** *//** *//** *//**
* @author Gavin.lee
* @date 09-4-30 17:20pm
* @version 1.0
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class LoadProperties
{

public void getSource(String source)
{
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(source);
Properties prop = new Properties();

try
{
prop.load(inputStream);

} catch (IOException e)
{
e.printStackTrace();
}
prop.setProperty("truename", "李顯武");
System.out.println("username:" + prop.getProperty("username") + ",password:" + prop.getProperty("password"));
System.out.println("truename:" + prop.getProperty("truename"));
prop.clear(); //只想加載用一次的話 可以清空
// System.out.println("truename:" + prop.getProperty("truename"));
// System.out.println("username:" + prop.getProperty("username") + ",password:" + prop.getProperty("password"));
}


public static void main(String[] args)
{

new LoadProperties()..getSource("config.properties").getProperty("username");
}

}
【二】可以指定properties 到自己想要的目錄下:

package com.Gavin.tools.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;


/** *//** *//** *//**
*
* @descripte 任意加載properties文件
* @author Gavin.lee
* @date 2009-6-7 0:55:18
* @version 1.0
*/

public class ReadConfig
{
static String path = WriteLog.class.getResource("/").getPath();
static String projectPath = path.substring(0, path.length()-16);// 到 WebRoot 下
private static final String PFILE = projectPath + "config.properties";

/** *//** *//** *//**
* 對應于屬性文件的文件對象變量
*/
private File m_file = null;


/** *//** *//** *//**
* 屬性文件的最后修改日期
*/
private long m_lastModifiedTime = 0;


/** *//** *//** *//**
* 屬性文件所對應的屬性對象變量
*/
private Properties m_props = null;

private static ReadConfig m_instance = new ReadConfig();


/** *//** *//** *//**
* 私有的構造器,用以保證外界無法直接實例化
*/

private ReadConfig()
{
m_file = new File(PFILE);
m_lastModifiedTime = m_file.lastModified();

if (m_lastModifiedTime == 0)
{
System.err.println(PFILE + " file does not exist!");
}
m_props = new Properties();

try
{
m_props.load(new FileInputStream(PFILE));

} catch (Exception e)
{
e.printStackTrace();
}
}


/** *//** *//** *//**
* 靜態工廠方法
* @return 返還ReadConfig 類的單一實例
*/

synchronized public static ReadConfig getInstance()
{
return m_instance;
}


/** *//** *//** *//**
* 讀取一特定的屬性項
*
* @param name 屬性項的項名
* @param defaultVal 屬性項的默認值
* @return 屬性項的值(如此項存在), 默認值(如此項不存在)
*/

public String getConfigItem(String name, String defaultVal)
{
long newTime = m_file.lastModified();
// 檢查屬性文件是否被其他程序
// 如果是,重新讀取此文件


if (newTime == 0)
{
// 屬性文件不存在

if (m_lastModifiedTime == 0)
{
System.err.println(PFILE + " file does not exist!");

} else
{
System.err.println(PFILE + " file was deleted!!");
}
return defaultVal;

} else if (newTime > m_lastModifiedTime)
{
// Get rid of the old properties
m_props.clear();

try
{
m_props.load(new FileInputStream(PFILE));

} catch (Exception e)
{
e.printStackTrace();
}
}
m_lastModifiedTime = newTime;
String val = m_props.getProperty(name);

if (val == null)
{
return defaultVal;

} else
{
return val;
}
}


/** *//** *//** *//**
* 讀取一特定的屬性項
*
* @param name 屬性項的項名
* @return 屬性項的值(如此項存在), 空(如此項不存在)
*/

public String getConfigItem(String name)
{
return getConfigItem(name, "");
}

public static void main (String args[])
{
System.out.println(ReadConfig.getInstance().getConfigItem("username"));
}
}
properties :
#url=wap.500wan.com
username = doingjava
password = lixianwu
birthday =1986-06-03
像這樣的properties文件,用上面的三個類都是任意可讀的。都在項目中試驗過,均可使用。 O(∩_∩)O~
,%%%%%%%%,
,%%/\%%%%/\%%
,%%%\c "" J/%%%
%. %%%%/ o o \%%%
`%%. %%%% _ |%%%
`%% `%%%%(__Y__)%%'
// ;%%%%`\-/%%%'
(( / `%%%%%%%'
\\ .' |
\\ / \ | |
\\/ ) | |
\ /_ | |__
(___________)))))))【這個獅子很形象哈,呵呵】