Java中读取src文件下属性文件(支持跨服务器)


  1. import java.io.File;     
  2. import java.io.FileInputStream;     
  3. import java.io.FileNotFoundException;     
  4. import java.io.IOException;     
  5. import java.io.InputStream;     
  6. import java.io.UnsupportedEncodingException;     
  7. import java.net.URLDecoder;     
  8. import java.util.Iterator;     
  9. import java.util.Properties;     
  10. import java.util.Set;     
  11. import java.util.logging.Level;     
  12. import java.util.logging.Logger;     
  13.      
  14. /**   
  15.  *   
  16.  * @author zcb   
  17.  */     
  18. public class Test {     
  19.      
  20.     public static void main(String args[]) {     
  21.      
  22.         Test test = new Test();     
  23.         InputStream in = null;     
  24.         Properties props = new Properties();     
  25.         //第一种方法,取得src下的属性文件,成功     
  26.         in = test.getClass().getResourceAsStream("/mypropertiestest.properties");     
  27.      
  28.         //第二种方法,取得src下的属性文件,相对第一种少了个“/”,注意:error,不行,此时取得的路径是到classes文件夹     
  29. //        System.out.println("path:"+test.getClass().getResource("mypropertiestest.properties").getPath());     
  30. //        in = test.getClass().getResourceAsStream("mypropertiestest.properties");     
  31.              
  32.      
  33.         //第三种种方法,通过绝对路径,取得src下的属性文件,成功,但对apusic服务器不大理想,属性文件要拷贝到项目外面     
  34. //        String filepath = test.getClass().getResource("/").getPath()  + java.io.File.separator + "mypropertiestest.properties";     
  35. //        try {     
  36. ////        filepath = filepath.substring(1).replaceAll("%20", " ");//or filepath = filepath.replaceAll("%20", " ");     
  37. //            filepath = URLDecoder.decode(filepath, "UTF-8");     
  38. //        } catch (UnsupportedEncodingException ex) {     
  39. //            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);     
  40. //        }     
  41. //        try {     
  42. //            in = new FileInputStream(new File(filepath));     
  43. //        } catch (FileNotFoundException ex) {     
  44. //            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);     
  45. //        }     
  46.         try {     
  47.             props.load(in);     
  48.         } catch (IOException e) {     
  49.             e.printStackTrace();     
  50.         } finally {     
  51.             if (in != null) {     
  52.                 try {     
  53.                     in.close();     
  54.                 } catch (IOException ex) {     
  55.                     Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);     
  56.                 }     
  57.             }     
  58.         }     
  59.      
  60.         //输出属性文件中的信息     
  61.         Set set = props.keySet();     
  62.         Iterator it = set.iterator();     
  63.         System.out.println("Begin ...");     
  64.         while (it.hasNext()) {     
  65.             String key = (String) it.next();     
  66.             System.out.println(key + "=" + props.getProperty(key));     
  67.         }     
  68.         System.out.println("End");     
  69.     }     
  70. }   

在Windows下测试通过,Linux没测试,需要进一步研究。

补充:使用 ClassLoader.getSystemResourceAsStream("/mypropertiestest.properties")和 Thread.currentThread().getContextClassLoader().getResourceAsStream("/mypropertiestest.properties") 读取src下的属性文件,通过测试,在windows和Linux下的tomcat和apusic都能成功。

相关内容