當前位置:首頁 » 編程語言 » javaproperties文件讀取

javaproperties文件讀取

發布時間: 2025-03-28 17:24:11

① 在java中如何讀取properties文件

使用java.util.Propertiesx0dx0ax0dx0a1、創建一個Properties對象。x0dx0a2、使用對象的load方法載入你的property文件。x0dx0a3、使用getProperty方法取值。x0dx0a例子:x0dx0apackage com.bill.test;x0dx0ax0dx0aimport java.io.FileInputStream;x0dx0aimport java.util.Properties;x0dx0ax0dx0apublic class Test {x0dx0apublic static void main(String[] args) throws Exception{x0dx0aProperties property = new Properties();x0dx0aproperty.load(new FileInputStream("你的文件位置"));x0dx0aString value = property.getProperty("你的屬性的key");x0dx0a//TODO 使用value...x0dx0a}x0dx0a}

② java讀取properties文件

Java讀取properties文件的方法比較多;
在最常用的讀取properties文件的方式--->「通過java.lang.Class類的getResourceAsStream(String name) 方法來實現」;
代碼:
InputStream in = getClass().getResourceAsStream("資源Name");

③ 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中如何讀取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();

熱點內容
數據存儲技術的發展 發布:2025-03-31 14:28:45 瀏覽:507
元組資料庫 發布:2025-03-31 14:25:00 瀏覽:712
手機文件android在哪 發布:2025-03-31 14:22:38 瀏覽:816
在我的世界中怎麼打開伺服器 發布:2025-03-31 14:20:25 瀏覽:465
目標伺服器ip地址怎麼看 發布:2025-03-31 14:18:48 瀏覽:724
印刷文件夾價格 發布:2025-03-31 14:14:40 瀏覽:850
七牛表單上傳 發布:2025-03-31 14:11:20 瀏覽:812
A軸怎麼編程 發布:2025-03-31 14:10:15 瀏覽:983
小說閱讀系統源碼 發布:2025-03-31 13:54:55 瀏覽:753
壓縮機內漏 發布:2025-03-31 13:54:48 瀏覽:648