JAVA讀取和寫入properties文件


1.讀取

        Properties prop = new Properties();
     try {
       //這個getResourceAsStream方法就是把文件轉為inputStream的方式 prop.load(HtpUtil.
class.getResourceAsStream("htp.properties")); } catch (IOException e) { e.printStackTrace(); }
     //獲取某個屬性 String host
= prop.getProperty("host");

2.寫入

        Properties prop = new Properties();
                                   //這兒是獲取文件的絕對路徑的。       
     FileOutputStream fileOutputStream = new FileOutputStream(CrackHash.class.getResource("crack.properties").getFile()); prop.setProperty("cookie", "123456");
     //store方法,第一個參數是輸入流,第二個是提示信息。#Sun Jan 07 18:47:58 CST 2018 prop.store(fileOutputStream,
new Date().toString()); fileOutputStream.flush(); fileOutputStream.close();

這個寫入以后,在eclipse里面不一定能立刻看到,先去文件夾里用nodepad++去看一下,寫入了就是寫入了,有時候重啟了eclipse依然看不到。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM