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. }  
Java代码
  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都能成功。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值