javaproperties讀取
① 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文件的幾種主要方法,還有其他的方法,基本上都大同小異。
② 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文件
../conf/config.properties
還有src是程序運行的根目錄,建議把所有與程序相關的都放到src里.不要放在外面
④ java怎麼讀取properties文件
最常用讀取properties文件的方法
InputStream
in
=
getClass().getResourceAsStream("資源Name");這種方式要求properties文件和當前類在同一文件夾下面。如果在不同的包中,必須使用:
InputStream
ins
=
this.getClass().getResourceAsStream(
⑤ 請教java如何實時讀取.properties文件中的配置項內容
Properties 類
static {
properties = new Properties();
InputStream in = null;
try {
in = ReadProperties.class.getResourceAsStream("/brochina-config.properties");//文件路徑
properties.load(in);
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(in!=null) in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String value = properties.getProperty(key);具體方法
⑥ 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文件的方法 InputStream in = getClass().getResourceAsStream("資源Name");這種方式要求properties文件和當前類在同一文件夾下面。如果在不同的包中,必須使用: InputStream ins = this.getClass().getResourceAsStream(
⑧ 在java中如何讀取properties文件
最常用讀取properties文件的方法
InputStream in = getClass().getResourceAsStream("資源Name");這種方式要求properties文件和當前類在同一文件夾下面。如果在不同的包中,必須使用:
InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");
Java中獲取路徑方法
獲取路徑的一個簡單實現
反射方式獲取properties文件的三種方式
1 反射方式獲取properties文件最常用方法以及思考:
Java讀取properties文件的方法比較多,網上最多的文章是"Java讀取properties文件的六種方法",但在Java應用中,最常用還是通過java.lang.Class類的getResourceAsStream(String name) 方法來實現,但我見到眾多讀取properties文件的代碼中,都會這么干:
InputStream in = getClass().getResourceAsStream("資源Name");
這裡面有個問題,就是getClass()調用的時候默認省略了this!我們都知道,this是不能在static(靜態)方法或者static塊中使用的,原因是static類型的方法或者代碼塊是屬於類本身的,不屬於某個對象,而this本身就代表當前對象,而靜態方法或者塊調用的時候是不用初始化對象的。
問題是:假如我不想讓某個類有對象,那麼我會將此類的默認構造方法設為私有,當然也不會寫別的共有的構造方法。並且我這個類是工具類,都是靜態的方法和變數,我要在靜態塊或者靜態方法中獲取properties文件,這個方法就行不通了。
那怎麼辦呢?其實這個類就不是這么用的,他僅僅是需要獲取一個Class對象就可以了,那還不容易啊--
取所有類的父類Object,用Object.class難道不比你的用你正在寫類自身方便安全嗎 ?呵呵,下面給出一個例子,以方便交流。
import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;
/**
* 讀取Properties文件的例子
* File: TestProperties.java
* User: leimin
* Date: 2008-2-15 18:38:40
*/
public final class TestProperties {
private static String param1;
private static String param2;
static {
Properties prop = new Properties();
InputStream in = Object. class .getResourceAsStream( "/test.properties" );
try {
prop.load(in);
param1 = prop.getProperty( "initYears1" ).trim();
param2 = prop.getProperty( "initYears2" ).trim();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 私有構造方法,不需要創建對象
*/
private TestProperties() {
}
public static String getParam1() {
return param1;
}
public static String getParam2() {
return param2;
}
public static void main(String args[]){
System.out.println(getParam1());
System.out.println(getParam2());
}
}
運行結果:
151
152
當然,把Object.class換成int.class照樣行,呵呵,大家可以試試。
另外,如果是static方法或塊中讀取Properties文件,還有一種最保險的方法,就是這個類的本身名字來直接獲取Class對象,比如本例中可寫成TestProperties.class,這樣做是最保險的方法
2 獲取路徑的方式:
File fileB = new File( this .getClass().getResource( "" ).getPath());
System. out .println( "fileB path: " + fileB);
2.2獲取當前類所在的工程名:
System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 獲取路徑的一個簡單的Java實現</span>
/**
*獲取項目的相對路徑下文件的絕對路徑
*
* @param parentDir
*目標文件的父目錄,例如說,工程的目錄下,有lib與bin和conf目錄,那麼程序運行於lib or
* bin,那麼需要的配置文件卻是conf裡面,則需要找到該配置文件的絕對路徑
* @param fileName
*文件名
* @return一個絕對路徑
*/
public static String getPath(String parentDir, String fileName) {
String path = null;
String userdir = System.getProperty("user.dir");
String userdirName = new File(userdir).getName();
if (userdirName.equalsIgnoreCase("lib")
|| userdirName.equalsIgnoreCase("bin")) {
File newf = new File(userdir);
File newp = new File(newf.getParent());
if (fileName.trim().equals("")) {
path = newp.getPath() + File.separator + parentDir;
} else {
path = newp.getPath() + File.separator + parentDir
+ File.separator + fileName;
}
} else {
if (fileName.trim().equals("")) {
path = userdir + File.separator + parentDir;
} else {
path = userdir + File.separator + parentDir + File.separator
+ fileName;
}
}
return path;
}
4 利用反射的方式獲取路徑:
InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" );
InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" );
InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );
⑨ java讀取properties文件
Java讀取properties文件的方法比較多;
在最常用的讀取properties文件的方式--->「通過java.lang.Class類的getResourceAsStream(String name) 方法來實現」;
代碼:
InputStream in = getClass().getResourceAsStream("資源Name");
⑩ 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();