properties
Properties文件是java中的一種配置文件,文件后綴為“.properties”,文件的內容格式是“key=value”的格式,用 # 作為注釋。
我的properties 文件放在路徑
寫與讀
向properties文件中寫入數據
1
2
3
4
5
6
7
8
9
10
|
//創建一個properties對象 Properties pro = new Properties(); //創建一個輸出流 里面路徑填寫文件的路徑 OutputStream proos = new FileOutputStream( "user.properties" ); pro.setProperty( "id" , "1001" ); pro.setProperty( "username" , "你好" ); pro.setProperty( "password" , "123" ); //將數據儲存到文件中,第一個參數是 輸出流,第二個參數是注釋 pro.store(proos, "User" ); proos.close(); |
將文件中的數據取出
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Properties pro = new Properties(); //創建一個輸出流 InputStream prois = new FileInputStream( "user.properties" ); //將文件取出 傳入一個 輸出流 pro.load(prois); int id = Integer.parseInt((String) pro.get( "id" )); String username = (String) pro.get( "username" ); String password = (String) pro.get( "password" ); System.out.println(pro); System.out.println(id); System.out.println(username); System.out.println(password); prois.close(); |
運行結果
總結
到此這篇關于Java中使用Properties配置文件的文章就介紹到這了,更多相關Java用Properties配置文件內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_44850016/article/details/108303568