一、properties檔案介紹
java中的properties檔案是一種配置檔案,主要用于表達配置資訊,檔案類型為*.properties,格式為文本檔案,檔案的内容是格式是 "鍵=值"的格式,在properties檔案中,可以用"#"來作注釋,properties檔案在Java程式設計中用到的地方很多,操作很友善。
properties檔案示例:
# 以下為伺服器、資料庫資訊
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
# 以下為資料庫表資訊
dbTable = mytable
# 以下為伺服器資訊
ip = 192.168.0.9
上面的檔案中我們假設該檔案名為:test.properties 檔案。其中# 開始的一行為注釋資訊;在等号“= ”左邊的我們稱之為key ;等号“= ”右邊的我們稱之為value 。(其實就是我們常說的鍵- 值對)key 應該是我們程式中的變量。而value 是我們根據實際情況配置的。
二、java常見讀取properties檔案方法
1、使用java.util.Properties類的load()方法示例:
Java代碼
- InputStream in = lnew BufferedInputStream(new FileInputStream(name));
- Properties p = new Properties();
- p.load(in);
<span style="font-size:14px;"><span style="font-size:14px;">InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);</span></span>
2、使用java.util.ResourceBundle類的getBundle()方法
示例:
Java代碼
- ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
<span style="font-size:14px;"><span style="font-size:14px;">ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); </span></span>
用ResourceBundle讀取.properties檔案可避免路徑問題
我在jar裡讀取.properties檔案時,總是找不到檔案路徑,後來用ResourceBundle讀取.properties檔案即可避免路徑問題,代碼如下:
//process為檔案名,切記不要加 .properties, URL是檔案裡的鍵名
Java代碼
- ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");
- String s = bundle.getString("URL");
- System.out.println(s);
- pURL = s;
<span style="font-size:14px;"><span style="font-size:14px;"> ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");
String s = bundle.getString("URL");
System.out.println(s);
pURL = s;</span></span>
3、使用java.util.PropertyResourceBundle類的構造函數
示例:
Java代碼
- InputStream in = new BufferedInputStream(new FileInputStream(name));
- ResourceBundle rb = new PropertyResourceBundle(in);
<span style="font-size:14px;"><span style="font-size:14px;">InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in); </span></span>
4、使用class變量的getResourceAsStream()方法
示例:
Java代碼
- InputStream in = 類名.class.getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
<span style="font-size:14px;"><span style="font-size:14px;">InputStream in = 類名.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in); </span></span>
5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法示例:
Java代碼
- InputStream in = 類名.class.getClassLoader().getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
<span style="font-size:14px;"><span style="font-size:14px;">InputStream in = 類名.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in); </span></span>
6、使用java.lang.ClassLoader類的getSystemResourceAsStream()靜态方法示例:
Java代碼
- InputStream in = ClassLoader.getSystemResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
<span style="font-size:14px;"><span style="font-size:14px;">InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in); </span></span>
7、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法示例:
Java代碼
- InputStream in = context.getResourceAsStream(path);
- Properties p = new Properties();
- p.load(in);
三、使用ResourceBundle讀取配置檔案
假如我現在有一個資料庫的配置檔案,我将它寫為資源檔案的樣式,則為:
| |
接下來,我們使用ResourceBundle類處理:
| |
可以這樣寫的原因,看下面源碼
/**
* Converts the given <code>bundleName</code> to the form required
* by the {@link ClassLoader#getResource ClassLoader.getResource}
* method by replacing all occurrences of <code>'.'</code> in
* <code>bundleName</code> with <code>'/'</code> and appending a
* <code>'.'</code> and the given file <code>suffix</code>. For
* example, if <code>bundleName</code> is
* <code>"foo.bar.MyResources_ja_JP"</code> and <code>suffix</code>
* is <code>"properties"</code>, then
* <code>"foo/bar/MyResources_ja_JP.properties"</code> is returned.
*
* @param bundleName
* the bundle name
* @param suffix
* the file type suffix
* @return the converted resource name
* @exception NullPointerException
* if <code>bundleName</code> or <code>suffix</code>
* is <code>null</code>
*/
public final String toResourceName(String bundleName, String suffix) {
StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length());
sb.append(bundleName.replace('.', '/')).append('.').append(suffix);
return sb.toString();
}
最終還是用classloader進行資源檔案的加載:
else if (format.equals("java.properties")) {
final String resourceName = toResourceName0(bundleName, "properties");
if (resourceName == null) {
return bundle;
}
final ClassLoader classLoader = loader;
final boolean reloadFlag = reload;
InputStream stream = null;
try {
stream = AccessController.doPrivileged(
new PrivilegedExceptionAction<InputStream>() {
public InputStream run() throws IOException {
InputStream is = null;
if (reloadFlag) {
URL url = classLoader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
// Disable caches to get fresh data for
// reloading.
connection.setUseCaches(false);
is = connection.getInputStream();
}
}
} else {
is = classLoader.getResourceAsStream(resourceName);
}
return is;
}
});
} catch (PrivilegedActionException e) {
throw (IOException) e.getException();
}
if (stream != null) {
try {
bundle = new PropertyResourceBundle(stream);
} finally {
stream.close();
}
}
} else {
throw new IllegalArgumentException("unknown format: " + format);
}
并且最終 還是使用了java類Properties
public PropertyResourceBundle (InputStream stream) throws IOException {
Properties properties = new Properties();
properties.load(stream);
lookup = new HashMap(properties);
}