java讀取配置文件properties
Ⅰ java怎麼讀取properties文件
/*** 用來測試獲取配置文件的類* @author BaiKeyang**/public class ResourceUtils {public static String getValue(String file, String key) { String value; // 未取到值,從properites文件中查找 try { ResourceBundle bundle = ResourceBundle.getBundle(file); value = bundle.getString(key); if (value==null){ throw new RuntimeException("沒有該屬性的值:"+key); } value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); return value; } catch (Throwable e) { System.err.println(e); return null; } }public static void main(String[] args) {String resourceFile = "config.dataBase";// 創建一個默認的ResourceBundle對象 // ResourceBundle會查找包config下的dataBase.properties的文件 // config是資源的包名,它跟普通java類的命名規則完全一樣: // - 區分大小寫 // - 擴展名 .properties 省略。就像對於類可以省略掉 .class擴展名一樣 // - 資源文件必須位於指定包的路徑之下(位於所指定的classpath中) // 假如你是在非Web項目中使用,則一定要寫資源文件的路徑,也就是包路徑必須存在。// 如果是Web項目,不寫包路徑可以,此時將資源文件放在WEB-INF\classes\目錄下就可以。String url = getValue(resourceFile, "db.url");System.out.println(url);}}
Ⅱ java怎麼調用properties配置文件的值
projectPath=11
aa= 11
public class ConfigTool {
static String projectPath = "";
public static HashMap<String,String> hashMap = new HashMap();
static {
Properties property = new Properties();
InputStream in = ConfigTool.class.getResourceAsStream("/config.properties");
try {
property.load(in);
projectPath = property.getProperty("projectPath").trim();
hashMap.put("huifu", property.getProperty("aa"));
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (null != in) {
try {
in.close();
} catch (Exception e) {}
}
}
}
}
Ⅲ java程序讀取properties配置文件出現中文亂碼
這個問題有兩種辦法:
第一種辦法:如樓上所說的那樣也可以,就是native2ascii -reverse -encoding gb2312 user.properties ActionName_zh_CN.properties這樣以後,你打開ActionName_zh_CN.properties的內容,再將ActionName_zh_CN.properties文件這樣置:native2ascii ActionName_zh_CN.properties userChange.properties 然後你的程序讀userChange.properties的內容就可以。
第二種辦法是:
如你代碼里寫的,你可以在String username=property.getProperty("username");之後,添加JAVA代碼頁可以將亂碼轉為中文的。用如下語句就可以了,resultName=new String(username.getBytes("ISO-8859-1"),"gbk"); 然後再用resultName就可以了,不過這樣的話你下面的String password=property.getProperty("password");
都慢慢的通過上面的java代碼去轉。
不知道你對反射熟悉不?如果熟悉的話可以通過反射機制去做第二種辦法的轉碼就方便多了!
Ⅳ 在java中如何讀取properties文件
使用java.util.Properties
1、創建一個Properties對象。
2、使用對象的load方法載入你的property文件。
3、使用getProperty方法取值。
例子:
package com.bill.test;
import java.io.FileInputStream;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws Exception{
Properties property = new Properties();
property.load(new FileInputStream("你的文件位置"));
String value = property.getProperty("你的屬性的key");
//TODO 使用value...
}
}
Ⅳ java中怎麼讀取properties文件
(1)load(InputStream inStream)
這個方法可以從.properties屬性文件對應的文件輸入流中,載入屬性列表到內Properties類對象。如下面容的代碼:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();
(2)store(OutputStream out, String comments)
這個方法將Properties類對象的屬性列表保存到輸出流中。如下面的代碼:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();
Ⅵ 求用java讀寫properties文件的代碼
Java可使用Properties類讀寫properties,具體說明如下:
1.Properties類與Properties配置文件
Properties類繼承自Hashtable類並且實現了Map介面,也是使用一種鍵值對的形式來保存屬性集。不過Properties有特殊的地方,就是它的鍵和值都是字元串類型。
2.Properties中的主要方法
(1)load(InputStream inStream)
這個方法可以從.properties屬性文件對應的文件輸入流中,載入屬性列表到Properties類對象。如下面的代碼:
Properties pro = new Properties();
FileInputStream in = new FileInputStream("a.properties");
pro.load(in);
in.close();
(2)store(OutputStream out, String comments)
這個方法將Properties類對象的屬性列表保存到輸出流中。如下面的代碼:
FileOutputStream oFile = new FileOutputStream(file, "a.properties");
pro.store(oFile, "Comment");
oFile.close();
如果comments不為空,保存後的屬性文件第一行會是#comments,表示注釋信息;如果為空則沒有注釋信息。
注釋信息後面是屬性文件的當前保存時間信息。
(3)getProperty/setProperty
這兩個方法是分別是獲取和設置屬性信息。
3.代碼實例
屬性文件a.properties如下:
name=root
pass=liu
key=value
讀取a.properties屬性列表,與生成屬性文件b.properties。代碼如下:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
try {
// 讀取屬性文件a.properties
InputStream in = new BufferedInputStream(new FileInputStream("a.properties"));
// /載入屬性列表
Properties prop = new Properties();
prop.load(in);
Iterator<String> it = prop.stringPropertyNames().iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + ":" + prop.getProperty(key));
}
in.close();
// /保存屬性到b.properties文件
FileOutputStream oFile = new FileOutputStream("b.properties", true);// true表示追加打開
prop.setProperty("phone", "10086");
prop.store(oFile, "The New properties file");
oFile.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Ⅶ java讀取properties配置文件路徑問題
可以直接通過Thread方法直接獲取到項目路徑下的配置文件:
static {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("BankConPort.properties"); //載入線程文件成為流
Properties prop = new Properties();
try {
prop.load(is);//直接轉換為對象
BOB_ReqURI = prop.getProperty("BOB_ReqURI");
BOB_SignURI = prop.getProperty("BOB_SignURI");
BOBLOGINPASSWORD = prop.getProperty("BOBLOGINPASSWORD");
} catch (IOException ex) {
java.util.logging.Logger.getLogger(BOBUtil.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.info("解析信息出錯", e.getMessage());
}
}
}
}
Ⅷ java代碼怎麼獲取properties文件的內容
importjava.util.Properties;
publicclassPropertiesUtil{
privatestaticPropertiesinit=null;
privatestaticPropertiesutil=null;
privatestaticPropertieschid=null;
(){
if(init==null){
try{
init=newProperties();
init.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("init.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returninit;
}
(){
if(util==null){
try{
util=newProperties();
util.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("util.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returnutil;
}
(){
if(chid==null){
try{
chid=newProperties();
chid.load(PropertiesUtil.class.getClassLoader().getResourceAsStream("chid.properties"));
}catch(Exceptione){
e.printStackTrace();
}
}
returnchid;
}
/**
*獲取屬性配置文件參數值
*@paramkey參數名稱
*@paramdef參數默認值
*@return參數值
*/
publicstaticStringget(Stringkey,Stringdef){
Stringval=getInit().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
publicstaticlonggetlong(Stringkey,longdef)
{
try{
def=Long.parseLong(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}
/**
*獲取屬性配置文件參數值
*@paramkey參數名稱
*@paramdef參數默認值
*@return參數值
*/
publicstaticintget(Stringkey,intdef){
try{
def=Integer.parseInt(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}
publicstaticlongget(Stringkey,longdef){
try{
def=Long.parseLong(getInit().getProperty(key));
}catch(Exceptione){
e.printStackTrace();
returndef;
}
returndef;
}
/**
*獲取屬性配置文件參數值
*@paramkey參數名稱
*@paramdef參數默認值
*@return參數值
*/
publicstaticStringgetUtil(Stringkey,Stringdef){
Stringval=getUtil().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
publicstaticlonggetUtil(Stringkey,longdef){
longval=Long.parseLong(getUtil().getProperty(key));
if(val==0){
returndef;
}
returnval;
}
/**
*獲取屬性配置文件參數值
*@paramkey參數名稱
*@paramdef參數默認值
*@return參數值
*/
(Stringkey,Stringdef){
Stringval=getChid().getProperty(key);
if(val==null||val.length()==0){
returndef;
}
returnval;
}
importcom.jinlou.util.PropertiesUtil;
publicclassTest{
publicstaticvoidmain(String[]args){
//從配置文件中去key=test的value如果配置文件中無此key則返回默認值
Stringtest=PropertiesUtil.get("test","默認值");
System.out.println(test);
}
}
Ⅸ 如何在java類中讀取Properties配置文件
最常用讀取properties文件的方法 InputStream in = getClass().getResourceAsStream("資源Name");這種方式要求properties文件和當前類在同一文件夾下面。如果在不同的包中,必須使用: InputStream ins = this.getClass().getResourceAsStream(