JAVA读取外部资源的方法

在java代码中经常有读取外部资源的要求:如配置文件等等,通常会把配置文件放在classpath下或者在web项目中放在web-inf下.

1.从当前的工作目录中读取:

  1. try {  
  2.             BufferedReader in = new BufferedReader(new InputStreamReader
  3. (new FileInputStream("wkdir.txt")));  
  4.             String str;  
  5.             while ((str = in.readLine()) != null) {  
  6.                 System.out.println(str);  
  7.             }  
  8.             in.close();  
  9.         } catch (IOException e) {  
  10.         }  
2,从classpath中读取(读取找到的第一个符合名称的文件):

  1. try {  
  2.             InputStream stream = ClassLoader.getSystemResourceAsStream("fileinjar.txt");  
  3.             BufferedReader in = new BufferedReader(new InputStreamReader(stream));  
  4.             String str;  
  5.             while ((str = in.readLine()) != null) {  
  6.                 System.out.println(str);  
  7.             }  
  8.             in.close();  
  9.         } catch (IOException e) {  
  10.         }  
3,从classpath中读取(读取找到的所有符合名称的文件,如Spring中带有classpath*:前缀的情况就会从classpath中遍历):

  1. try {  
  2.   
  3.             Enumeration resourceUrls = Thread.currentThread().getContextClassLoader().
  4. getResources("fileinjar.txt");  
  5.   
  6.             while (resourceUrls.hasMoreElements()) {  
  7.                 URL url = (URL) resourceUrls.nextElement();  
  8.                 System.out.println(url);  
  9.   
  10.                 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));  
  11.                 String str;  
  12.                 while ((str = in.readLine()) != null) {  
  13.                     System.out.println(str);  
  14.                 }  
  15.                 in.close();  
  16.   
  17.             }  
  18.   
  19.         } catch (IOException e) {  
  20.         }  
4,从URL中读取:

  1. try {  
  2.   
  3.             URL url = new URL("http://blog.csdn.net/kkdelta");  
  4.             System.out.println(url);  
  5.   
  6.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));  
  7.             String str;  
  8.             while ((str = in.readLine()) != null) {  
  9.                 System.out.println(str);  
  10.             }  
  11.             in.close();  
  12.   
  13.         } catch (IOException e) {  
  14.             e.printStackTrace();  
  15.         }  
5,web项目从web-inf文件夹读取(通过得到ServletContext读取,可以在servlet或者能够得到request的类中使用):

  1. try {  
  2.   
  3.             URL url = (URL) getServletContext().getResource("/WEB-INF/webinffile.txt");  
  4.             // URL url = (URL)req.getSession().getServletContext().
  5.     // getResource("/WEB-INF/webinffile.txt");  
  6.             System.out.println(url);  
  7.   
  8.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));  
  9.             String str;  
  10.             while ((str = in.readLine()) != null) {  
  11.                 System.out.println(str);  
  12.             }  
  13.             in.close();  
  14.   
  15.         } catch (IOException e) {  
  16.             e.printStackTrace();  
  17.         }  
以上代码在eclipse环境中运行测试过.不过最近在用JUnit的时候,通过ant运行JUnit时通过

ClassLoader.getSystemResourceAsStream("file.txt");的方式去找不到文件.

改成 Xclass.class.getClassLoader().getResourceAsStream("file.txt");

能从ant指定的classpath中找到文件.原因是ClassLoader和Xclass.class.getClassLoader()是不同的,查找的路径不一样.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值