JAVA Properties配置文件的讀寫


  通常我們就會看到一個配置文件,比如:jdbc.properties,它是以“.properties”格式結尾的。在java中,這種文件的內容以鍵值對<key,value>存儲,通常以“=”分隔key和value,當然也可以用":"來分隔,但通常不這么干。

  • 讀取配置文件

  這里有一個文件叫asfds.properties,里面簡單的存了兩個鍵值對,如下圖所示:

  

  讀取配置文件的基本步驟是:

  1. 實例化一個Properties對象;
  2. 將要讀取的文件放入數據流中;
  3. 調用Properties對象的load方法,將屬性文件的鍵值對加載到Properties類對象中;
  4. 調用Properties對象的getProperty(String key)讀入對應key的value值。

  注:如果想要讀取key值,可以調用Properties對象的stringPropertyNames()方法獲取一個set集合,然后遍歷set集合即可。

  讀取配置文件的方法: 

 1     /**
 2      * read properties file
 3      * @param paramFile file path
 4      * @throws Exception
 5      */
 6     public static void inputFile(String paramFile) throws Exception
 7     {
 8         Properties props=new Properties();//使用Properties類來加載屬性文件
 9         FileInputStream iFile = new FileInputStream(paramFile);
10         props.load(iFile);
11 
12         /**begin*******直接遍歷文件key值獲取*******begin*/
13         Iterator<String> iterator = props.stringPropertyNames().iterator();
14         while (iterator.hasNext()){
15             String key = iterator.next();
16             System.out.println(key+":"+props.getProperty(key));
17         }
18         /**end*******直接遍歷文件key值獲取*******end*/
19 
20         /**begin*******在知道Key值的情況下,直接getProperty即可獲取*******begin*/
21         String user=props.getProperty("user");
22         String pass=props.getProperty("pass");
23         System.out.println("\n"+user+"\n"+pass);
24         /**end*******在知道Key值的情況下,直接getProperty即可獲取*******end*/
25         iFile.close();
26 
27     }
  • 寫入配置文件

  寫入配置文件的基本步驟是:

  1. 實例化一個Properties對象;
  2. 獲取一個文件輸出流對象(FileOutputStream);
  3. 調用Properties對象的setProperty(String key,String value)方法設置要存入的鍵值對放入文件輸出流中;
  4. 調用Properties對象的store(OutputStream out,String comments)方法保存,comments參數是注釋;

  寫入配置文件的方法:

 1 /**
 2      *write properties file
 3      * @param paramFile file path
 4      * @throws IOException
 5      */
 6     private static void outputFile(String paramFile) throws IOException {
 7         ///保存屬性到b.properties文件
 8         Properties props=new Properties();
 9         FileOutputStream oFile = new FileOutputStream(paramFile, true);//true表示追加打開
10         props.setProperty("testKey", "value");
11         //store(OutputStream,comments):store(輸出流,注釋)  注釋可以通過“\n”來換行
12         props.store(oFile, "The New properties file Annotations"+"\n"+"Test For Save!");
13         oFile.close();
14     }
  • 測試輸出

  文件讀取:

  @Test
    public void testInputFile(){//read properties file
        try {
            inputFile("resources/asfds.properties");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  輸出:

  

  文件寫入:

    @Test
    public void testOutputFile(){//write properties file
        try {
            outputFile("resources/test.properties");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    寫入的文件:

   

 


免責聲明!

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



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