クラスローダリソースファイルの取得



  
  
  
  
  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. }