首先在web工程src目錄下新建一個database.properties 文件
內容如下:
user=root
password=root
databaseType=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.2.232:3306/oa? seUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull
這里的內容隨自己的合適而變化,這里不多說了;
在新建一個讀取.properties文件新類:
package com.junhai.tamsys.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class DatabaseConfigure {
private Properties property;
private FileInputStream inputFile;
private FileOutputStream outputFile;
public DatabaseConfigure() {
property = new Properties();
}
public DatabaseConfigure(String filePath) {
property = new Properties();
try {
inputFile = new FileInputStream(filePath);
property.load(inputFile);
inputFile.close();
} catch (FileNotFoundException e) {
System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 重載函數(shù),得到key的值 @param key 取得其值的鍵 @return key的值
*/
public String getValue(String key) {
if (property.containsKey(key)) {
return property.getProperty(key);
} else
return "";
}
/*
* 重載函數(shù),得到key的值
*
* @param fileName propertys文件的路徑+文件名 @param key 取得其值的鍵 @return key的值
*/
public String getValue(String fileName, String key) {
try {
String value = "";
inputFile = new FileInputStream(fileName);
property.load(inputFile);
inputFile.close();
if (property.containsKey(key)) {
value = property.getProperty(key);
return value;
} else
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
/*
* 清除properties文件中所有的key和其值
*/
public void clear() {
property.clear();
}
/*
* 改變或添加一個key的值,當key存在于properties文件中時該key的值被value所代替, 當key不存在時,該key的值是value
* @param key 要存入的鍵 @param value 要存入的值
*/
public void setValue(String key, String value) {
property.setProperty(key, value);
}
/*
* 將更改后的文件數(shù)據(jù)存入指定的文件中,該文件可以事先不存在。 @param fileName 文件路徑+文件名稱 @param
* description 對該文件的描述
*/
public void saveFile(String fileName, String description) {
try {
outputFile = new FileOutputStream(fileName);
property.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] args) {
DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");
System.out.println(test.getValue("user"));
System.out.println(test.getValue("databaseType")+";"+test.getValue("url"));
}
}
這樣就可以通過key得到相應的value了;
想在這里多說一點是路徑問題,java工程和web 工程讀取.properties路徑是不一樣的,我在這里就花了不少時間。
JAVA工程: DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");這樣讀取就可以了:
web工程這樣讀取:DatabaseConfigure dc = new DatabaseConfigure(Thread.currentThread().getContextClassLoader()
.getResource("").getPath()+"database.properties");
因為當服務器啟動后工程里面東西會編譯后加到\WEB-INF\classes這個目錄,服務也是從這個目錄下讀取信息的。所以先取到這個路徑,才能正確讀取到database.properties這里邊的內容。