手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2011-05-31] 来源:Linux社区  作者:ahhsxy [字体:大 中 小]
  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都能成功。

linux
相关资讯       Java 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款