java讀取properties文件
關鍵字: java讀取properties配置文件 今天要將以前一個項目的數據連接改成從properties文件中配置。找了好久,網上也不少,可就是讀取文件時總是找不到文件在哪(我放在classes目錄下了)。最後終於成功,記下,以做紀念。
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;
public class Conn {
private String fileName="/db.properties";//這裏是指放在classes下,如果有包的話,前麵加包名即可。例:/com/web/db.properties
private String driver = "";
private String url = "";
private String username ="";
private String password = "";
Connection conn=null;
public Connection getConn(){
Properties p = new Properties();
try {
InputStream in = Conn.class.getResourceAsStream(fileName);// 與類在同一級,也可用/package/path/to/fileName
p.load(in);
in.close();
if(p.containsKey("driver")){
this.driver = p.getProperty("driver");
}
if(p.containsKey("url")){
this.url = p.getProperty("url");
}
if(p.containsKey("user")){
this.username = p.getProperty("user");
}
if(p.containsKey("password")){
this.password = p.getProperty("password");
}
} catch (IOException ex) {
Logger.getLogger(Conn.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(p.getProperty("driver"));
try {
Class.forName(this.driver);
conn = DriverManager.getConnection(this.url,this.username,this.password);
} catch (SQLException ex) {
ex.printStackTrace();
System.out.print("獲取連接異常");
} catch (ClassNotFoundException ex) {
System.out.print("加載驅動出錯");
ex.printStackTrace();;
}
return conn;
}
}
package com.web.connection; 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; public class Conn { private String fileName="/db.properties";//這裏是指放在classes下,如果有包的話,前麵加包名即可。例:/com/web/db.properties private String driver = ""; private String url = ""; private String username =""; private String password = ""; Connection conn=null; public Connection getConn(){ Properties p = new Properties(); try { InputStream in = Conn.class.getResourceAsStream(fileName);//這裏有人用new FileInputStream(fileName),不過這種方式找不到配置文件。有人說是在classes下,我調過了,不行。 p.load(in); in.close(); if(p.containsKey("driver")){ this.driver = p.getProperty("driver"); } if(p.containsKey("url")){ this.url = p.getProperty("url"); } if(p.containsKey("user")){ this.username = p.getProperty("user"); } if(p.containsKey("password")){ this.password = p.getProperty("password"); } } catch (IOException ex) { Logger.getLogger(Conn.class.getName()).log(Level.SEVERE, null, ex); } System.out.println(p.getProperty("driver")); try { Class.forName(this.driver); conn = DriverManager.getConnection(this.url,this.username,this.password); } catch (SQLException ex) { ex.printStackTrace(); System.out.print("獲取連接異常"); } catch (ClassNotFoundException ex) { System.out.print("加載驅動出錯"); ex.printStackTrace();; } return conn; } }
網上文章常見的幾種讀取.properties文件的方式
1、使用java.util.Properties類的load()方法 示例:
Java代碼
- InputStream in = lnew BufferedInputStream(new FileInputStream(name));
- Properties p = new Properties();
- p.load(in);
InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in);
2、使用java.util.ResourceBundle類的getBundle()方法
示例:
Java代碼
- ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
用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;
ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process"); String s = bundle.getString("URL"); System.out.println(s); pURL = s;
3、使用java.util.PropertyResourceBundle類的構造函數
示例:
Java代碼
- InputStream in = new BufferedInputStream(new FileInputStream(name));
- ResourceBundle rb = new PropertyResourceBundle(in);
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in);
4、使用class變量的getResourceAsStream()方法
示例:
Java代碼
- InputStream in = 類名.class.getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
InputStream in = 類名.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);
5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 示例:
Java代碼
- InputStream in = 類名.class.getClassLoader().getResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
InputStream in = 類名.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in);
6、使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法 示例:
Java代碼
- InputStream in = ClassLoader.getSystemResourceAsStream(name);
- Properties p = new Properties();
- p.load(in);
InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in);
7、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 示例:
Java代碼
- InputStream in = context.getResourceAsStream(path);
- Properties p = new Properties();
- p.load(in);
最後更新:2017-04-02 06:51:30