properties讀取java
1. 請教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);具體方法
2. 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();
3. java 怎麼讀取配置文件
一.讀取xml配置文件
(一)新建一個java bean(HelloBean. java)
java代碼
(二)構造一個配置文件(beanConfig.xml)
xml 代碼
(三)讀取xml文件
1.利用
java代碼
2.利用FileSystemResource讀取
java代碼
二.讀取properties配置文件
這里介紹兩種技術:利用spring讀取properties 文件和利用java.util.Properties讀取
(一)利用spring讀取properties 文件
我們還利用上面的HelloBean. java文件,構造如下beanConfig.properties文件:
properties 代碼
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
屬性文件中的"helloBean"名稱即是Bean的別名設定,.class用於指定類來源。
然後利用org.springframework.beans.factory.support.來讀取屬性文件
java代碼
(二)利用java.util.Properties讀取屬性文件
比如,我們構造一個ipConfig.properties來保存伺服器ip地址和埠,如:
properties 代碼
ip=192.168.0.1
port=8080
三.讀取位於Jar包之外的properties配置文件
下面僅僅是列出讀取文件的過程,剩下的解析成為properties的方法同上
1 FileInputStream reader = new FileInputStream("config.properties");
2 num = reader.read(byteStream);
3 ByteArrayInputStream inStream = new ByteArrayInputStream(byteStream, 0, num);
四.要讀取的配置文件和類文件一起打包到一個Jar中
String currentJarPath = URLDecoder.decode(YourClassName.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //獲取當前Jar文件名,並對其解碼,防止出現中文亂碼
JarFile currentJar = new JarFile(currentJarPath);
JarEntry dbEntry = currentJar.getJarEntry("包名/配置文件");
InputStream in = currentJar.getInputStream(dbEntry);
//以上YourClassName是class全名,也就是包括包名
修改:
JarOutputStream out = new FileOutputStream(currentJarPath);
out.putNextEntry(dbEntry);
out.write(byte[] b, int off, int len); //寫配置文件
。。。
out.close();
4. java怎麼獲取properties文件中的值
按照讀取屬性文件的方法即可,如下:
5. 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();
6. java讀取properties配置文件路徑問題
說說我的項目中的情況吧:
配置文件「weblogic11g.properties」保存在WEB-INFO目錄下,和web.xml在同一個目錄下。
一個JavaBean專門用於讀取配置文件的內容:
public class PropertiesIO {
private String fileName = null;
public PropertiesIO(String fileName){
this.fileName = getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName;
}
public String getValue(String key){
try{
InputStream in = new FileInputStream(fileName);
Properties prop = new Properties();
prop.load(in);
in.close();
return prop.getProperty(key);
}
catch(Exception err){
err.printStackTrace();
return null;
}
}
}
重點說明:getClass().getClassLoader().getResource("/")會得到當前項目下的「WEB-INF\classes」目錄,即JavaBean的*.class文件的根目錄,
getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName
就會得到當前項目下的「WEB-INF\weblogic11g.properties」文件。
getValue()是根據鍵值得到相應配置項的內容,這樣就簡單了。
7. 在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" );