java路径问题(一)——getClass().getResourceAsStream()

src(源文件夹)

┣━11.properties

┗━myspider(myspider包)
         ┃
         ┣━22.properties
         ┗━Test.java


Java代码   收藏代码


  1. package myspider;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4.   
  5. /** 
  6.  * 
  7.  * @author mark 
  8.  */  
  9. public class Test {  
  10.     public static void main(String[] args) throws UnsupportedEncodingException{  
  11.         Test t=new Test();  
  12.         //文件名前不加“/”,则表示从当前类所在的包下查找该资源。如下则表示的是从包myspider下查找22.properties文件资源。  
  13.         System.out.println("1:"+t.getClass().getResourceAsStream("22.properties"));//输出java.io.BufferedInputStream@61de33  
  14.   
  15.         //文件名前加了“/”,则表示从类路径下也就是从classes文件夹下查找资源,如下表示从classes文件夹下查找22.properties文件资源。  
  16.         System.out.println("2:"+t.getClass().getResourceAsStream("/22.properties"));//输出null  
  17.   
  18.         //文件名前加了“/”,则表示从类路径下也就是从classes文件夹下查找资源,如下表示从classes文件夹下查找11.properties文件资源。  
  19.         System.out.println("3:"+t.getClass().getResourceAsStream("/11.properties"));//输出java.io.BufferedInputStream@14318bb  
  20.         System.out.println();  
  21.   
  22.         //当前包路径4:file:/E:/myobject/myspider/build/classes/myspider/  
  23.         System.out.println("4:"+t.getClass().getResource(""));  
  24.   
  25.         //输出当前类路径5:file:/E:/myobject/myspider/build/classes/  
  26.         System.out.println("5:"+t.getClass().getResource("/"));  
  27.   
  28.         /* 
  29.          * 如果类路径下的当前包有22.properties文件,则输出6:file:/E:/myobject/myspider/build/classes/myspider/22.properties 
  30.          * 否者输出源文件下的22.properties文件的路径,则输出:6:file:/E:/myobject/myspider/src/myspider/22.properties 
  31.          */  
  32.         System.out.println("6:"+t.getClass().getResource("22.properties"));  
  33.         /* 
  34.          * 如果类路径下有11.properties文件,则输出7:file:/E:/myobject/myspider/build/classes/11.properties 
  35.          * 否者输出源文件下的11.properties文件的路径,则输出:6:7:file:/E:/myobject/myspider/src/11.properties 
  36.          */  
  37.         System.out.println("7:"+t.getClass().getResource("/11.properties"));  
  38.   
  39.     }  
  40. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值