java 讀取配置文件 直接上代碼
java代碼
package test;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Properties;
public class Test {
public static void
main(String[] args) {
//獲取目錄路徑(App程序下獲取方法)
String path = new
Test().getClass().getResource("/").getPath();
//獲取目錄路徑(Web程序下獲取方法)
// String path = new
Test().getClass().getProtectionDomain().getCodeSource()
// .getLocation().getPath();
// if (path.indexOf("WEB-INF")
> 0) {
// path
= path.substring(0, path.indexOf("WEB-INF") +
8);
// } else
{
// throw new
IllegalAccessException("路徑獲取錯誤");
// }
FileInputStream fileInputStream = null;
Properties properties = null;
try {
//獲取文件路徑
String filepath = MessageFormat.format("{0}{1}",
path, "db2-comfig.properties");
//System.out.println(filepath);
filepath = java.net.URLDecoder.decode(filepath, "utf-8");
fileInputStream = new FileInputStream(filepath);
properties = new Properties();
properties.load(fileInputStream);
//獲取文件中的屬性(db2url爲文件屬性)的值
String DB2url = properties.getProperty("db2url");
//System.out.println(DB2url);
}
catch (Exception e) {