Java中使用Properties配置文件的簡單方法
properties
Properties文件是java中的一種配置文件,文件后綴為“.properties”,文件的內(nèi)容格式是“key=value”的格式,用 # 作為注釋。
我的properties 文件放在路徑
寫與讀
向properties文件中寫入數(shù)據(jù)
//創(chuàng)建一個properties對象Properties pro = new Properties();//創(chuàng)建一個輸出流 里面路徑填寫文件的路徑OutputStream proos = new FileOutputStream('user.properties');pro.setProperty('id', '1001');pro.setProperty('username','你好');pro.setProperty('password', '123');//將數(shù)據(jù)儲存到文件中,第一個參數(shù)是 輸出流,第二個參數(shù)是注釋pro.store(proos,'User');proos.close();
將文件中的數(shù)據(jù)取出
Properties pro = new Properties();//創(chuàng)建一個輸出流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();
運行結(jié)果
總結(jié)
到此這篇關(guān)于Java中使用Properties配置文件的文章就介紹到這了,更多相關(guān)Java用Properties配置文件內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. jsp文件下載功能實現(xiàn)代碼2. 詳解瀏覽器的緩存機(jī)制3. python多線程和多進(jìn)程關(guān)系詳解4. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享5. phpstudy apache開啟ssi使用詳解6. ajax請求后臺得到j(luò)son數(shù)據(jù)后動態(tài)生成樹形下拉框的方法7. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程8. JSP之表單提交get和post的區(qū)別詳解及實例9. Xml簡介_動力節(jié)點Java學(xué)院整理10. 存儲于xml中需要的HTML轉(zhuǎn)義代碼
