㈠ 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中如何讀取src下所有的properties文件
1.使用java.util.Properties類的load()方法
示例:
//文件在項目下。不是在包下!!
InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;
Properties p = new Properties();
p.load(in) ;
String className2 = p.getProperty("database.driver");
String url = p.getProperty("database.url");
String user = p.getProperty("database.user");
String password = p.getProperty("database.pass");
總結:如果是 在WEB上讀取properties文件,寫成下面這種。上面寫的那些只在 JavaSE 中
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println(path);
InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));
Properties prop = new Properties();
㈢ java中怎麼讀取properties
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class PropertiesTest
{
/*定義靜態方法,類名可以直接調用,用於讀取properties屬性文件中的內容*/
public static void initFile()
{
/*D://a.properties源屬性文件的路徑和名字*/
File file = new File("D://a.properties");
FileInputStream fis = null;
try
{
/*輸入流和屬性文件關聯*/
fis = new FileInputStream(file);
/*創建屬性集對象*/
Properties prop = new Properties();
/*將讀取的內容載入到屬性集對象中*/
prop.load(fis);
/*返回屬性列表中所有鍵的枚舉*/
Enumeration enums = prop.propertyNames();
while (enums.hasMoreElements())
{
/*將每一條屬性強制轉換為String類型,得到鍵key*/
String key = (String) enums.nextElement();
/*根據鍵得到對應的值value(String類型)*/
String value = prop.getProperty(key);
/*輸出properties屬性文件的內容*/
System.out.println(key+"----"+value);
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
/*調用方法讀取屬性文件中內容*/
PropertiesTest.initFile();
}
}
結果是:
UserPass----
URl----jdbc:microsoft:sqlserver://localhost:1433;databasename=employee
Driver----com.microsoft.jdbc.sqlserver.SQLServerDriver
UserName----sa
㈣ java怎麼properties的方法
properties是配置文件,主要的作用是通過修改配置文件可以方便的修改代碼中的參數,實現不用改class文件即可靈活變更參數。
解釋:java運行中java文件會變成class文件,之後無法通過反編譯找到原樣的代碼,這樣的話,如果java類中某個參數變更,就很難靈活的實現參數修改,這個時候properties 文件就能很靈活的實現配置,減少代碼的維護成本和提高開發效率。
㈤ java讀取properties文件
InputStream in = getProperties.class.getClassLoader().getResourceAsStream(
"config.properties");
這一句換個寫法試試:
Properties props = new Properties();
String url = this.getClass().getClassLoader().getResource(
"config.properties").toString().substring(6);
String empUrl = url.replace("%20", " ");// 如果你的文件路徑中包含空格,是必定會報錯的
System.out.println(empUrl);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(empUrl));
props.load(in);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
我就是一直這么寫的,沒問題。
我猜你讀取文件為空的原因,是你的文件路徑有空格。
㈥ 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
/**
* 讀取項目resource下的config文件夾中的配置屬性文件
*/
private static void setConfigMap() {
String filePath = classPath + configPath;
filePath = PathUtils.rebuild(filePath);
List<String> list = findFiles(filePath);
Map<String, String> tmpConfigMap = new HashMap<String, String>();
for (String configName : list) {
Properties props = getProperties(filePath + configName);
loadPropToMap(props);
}
}
㈧ java如何讀取.properties文件的數據
在prop包下建立LoadProp.java文件。 3.有很多方法來讀取.properties文件,現將主要方法羅列出來: a.通過class的getResourceAsStream()方法來讀取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } 一定要注意的是,class里的getResourceAsStream()方法里參數的類路徑一定要在前面加上"/",否則會報錯 b.使用class的getClassLoader()方法所得的ClassLoader的getResourceAsStream()來讀取 package prop; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadProp { public static void main(String[] args) { LoadProp loadProp = new LoadProp(); InputStream in = loadProp.getClass().getClassLoader().getResourceAsStream("config/a.properties"); Properties prop = new Properties(); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } System.out.println(prop.getProperty("name", "none")); System.out.println(prop.getProperty("age", "none")); } } ClassLoader的getResourceAsStream()方法與Class的getResourceAsStream()方法有點區別,在這里一定不要在類路徑前面加上"/",否則會報錯,是不是很奇怪。 c.使用ResourceBundle來讀取 package prop; import java.util.ResourceBundle; public class LoadProp { public static void main(String[] args) { ResourceBundle rb = ResourceBundle.getBundle("config/a"); System.out.println(rb.getString("name")); System.out.println(rb.getString("age")); } } 注意,getBundle()方法里的參數,是baseName,不要把後綴名寫出來,並且不要加"/"。 好了,這是讀取.properties文件的幾種主要方法,還有其他的方法,基本上都大同小異。