類加載器獲取資源文件

 

  1. package cn.lxl.servlet;  
  2.  
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import java.util.Properties;  
  6.  
  7. public class Person {  
  8.     public void update() throws IOException {  
  9.  
  10.         Properties ps1 = new Properties();  
  11.         Properties ps2 = new Properties();  
  12.  
  13.         // 用類加載器獲取獲取資源文件,獲取的內容是服務器重啓之前保存的數據  
  14.         ps1.load((Person.class.getClassLoader()  
  15.                 .getResourceAsStream("config.properties")));  
  16.  
  17.         String value1 = ps1.getProperty("name");  
  18.         System.out.println("ps1-->" + value1);  
  19.  
  20.         // 用類加載器獲取資源文件位置,獲取的數據是最新的  
  21.         FileInputStream fis = new FileInputStream(Person.class.getClassLoader()  
  22.                 .getResource("config.properties").getPath());  
  23.         ps2.load(fis);  
  24.  
  25.         String value2 = ps2.getProperty("name");  
  26.         System.out.println("ps2-->" + value2);  
  27.  
  28.     }  
  29. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章