閱讀918 返回首頁    go 阿裏雲 go 技術社區[雲棲]


java讀取properties文件

關鍵字: java讀取properties配置文件 今天要將以前一個項目的數據連接改成從properties文件中配置。找了好久,網上也不少,可就是讀取文件時總是找不到文件在哪(我放在classes目錄下了)。最後終於成功,記下,以做紀念。

Java代碼
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);// 與類在同一級,也可用/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代碼
  1. InputStream in = lnew BufferedInputStream(new FileInputStream(name));   
  2. Properties p = new Properties();   
  3. p.load(in);  
InputStream in = lnew BufferedInputStream(new FileInputStream(name));  Properties p = new Properties();  p.load(in);



2、使用java.util.ResourceBundle類的getBundle()方法
示例:

Java代碼
  1. ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());   
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());



用ResourceBundle讀取.properties文件可避免路徑問題
我在jar裏讀取.properties文件時,總是找不到文件路徑,後來用ResourceBundle讀取.properties文件即可避免路徑問題,代碼如下:


//process為文件名,切記不要加 .properties, URL是文件裏的鍵名

Java代碼
  1.     ResourceBundle bundle = ResourceBundle.getBundle("com.ihandy.smsoc.app.process");  
  2.    String s = bundle.getString("URL");  
  3. System.out.println(s);  
  4. 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代碼
  1. InputStream in = new BufferedInputStream(new FileInputStream(name));   
  2. ResourceBundle rb = new PropertyResourceBundle(in);   
InputStream in = new BufferedInputStream(new FileInputStream(name));  ResourceBundle rb = new PropertyResourceBundle(in);



4、使用class變量的getResourceAsStream()方法
示例:

Java代碼
  1. InputStream in = 類名.class.getResourceAsStream(name);   
  2. Properties p = new Properties();   
  3. p.load(in);   
InputStream in = 類名.class.getResourceAsStream(name);  Properties p = new Properties();  p.load(in);


5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 示例:

Java代碼
  1. InputStream in = 類名.class.getClassLoader().getResourceAsStream(name);   
  2. Properties p = new Properties();   
  3. p.load(in);   
InputStream in = 類名.class.getClassLoader().getResourceAsStream(name);  Properties p = new Properties();  p.load(in);



6、使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法 示例:

Java代碼
  1. InputStream in = ClassLoader.getSystemResourceAsStream(name);   
  2. Properties p = new Properties();   
  3. p.load(in);   
InputStream in = ClassLoader.getSystemResourceAsStream(name);  Properties p = new Properties();  p.load(in);



7、Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 示例:

Java代碼
  1. InputStream in = context.getResourceAsStream(path);   
  2. Properties p = new Properties();   
  3. p.load(in);  

最後更新:2017-04-02 06:51:30

  上一篇:go 推薦兩個Magento做的中文網站 GAP和佰草集
  下一篇:go 內網FTP服務器架設不完全解析