j2se----properties 和路径路径路径问题

[code]
/**
Properties 类是对HashTable的封装,他可以被看出放在硬盘中的HashMap

注意:配置文件里面的key和value是这样的
aaakey=afafdafdsf
*/
1。 Properties props = new Properties();
props.load(Test.class.getClassLoader()
.getResourceAsStream("com/zhe/aaa.properties"));
String value = props.getProperty("aaaKey");
这种方式是用类加载器去加载普通文件,本来类加载器是用来加载.class文件的,但是既然能加载.class文件,那么也就能加载普通文件,不过普通文件必须在类路径中,他会到类路径中逐一的寻找需要加载的文件,注意,我们在src下的东西.java都变成.class,其他不变,然后被全部移到bin目录下(web工程就是web root目录下的classes目录)。。。。。类加载器只能读不能写哈。。。如果要写的话还得用下面这种

再说一句:struts,spring这些框架的配置文件都是用类加载器来读取的,所以他们都是放在bin目录下,所以我们都去src目录下找哈。。。。

File f = new File(TestCobertura.class.getClassLoader().getResource(("com/test/zwz/test1/1.abc").getFile());


或者 InputStream ips = new FileInputStream("aaa.properties");
Properties props = new Properties();
props.load(ips);
.....

这里aaa.properties的位置是与src平级的地方

2。请辨别下面两个的区别:

Test.class.getClassLoader()
.getResourceAsStream("com/zhe/aaa.properties");//指定包,只能一级一级的找,不能相对,我们想象getClassLoader()就是得到根(bin或者classes)

Test.class.getResourceAsStream("aaa.properties");//有相对绝对的概念
第一种的比喻: 你每次找我要可乐,然后我就每次跑到可乐供应商那里去拿到可乐然后给你

第二种的比喻:你每次都找我要可乐,然后我灵机一动,批发了一箱可乐,然后卖给你。。。

相对的时候是相对于这个类自己的上级目录

Test.class.getResourceAsStream("/aaa.properties");这种方式是绝对的做法,这里的/代表webRoot/,所以我们需要加载meta-inf下面的东西时可以这样:getClass().getResourceAsStream(/META-INF/a.txt); 或者是bin...

区别于:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
String contextClassLoader = loader.getResource("/").toString();

这个可以获得当前线程的啊~~~


Test.class.getResourceAsStream("resouces/aaa.properties");//这还是相对的做法,resouces前面相当于接了Test所在的包

Test.class.getResource("") //因为是使用的相对路径,那么会在当前Test.class的位置来找,又因为什么都没有填,所以是找目录,那么就打印这个路径了。。。形如:file:/D:/program/openeap_devstudio/workspace/MyTest/bin/com/abc/
Test.class.getResource("/")//因为是绝对路径,那么一开始就跳到了bin目录上开始找,因为是找目录,所以会打印根目录的地址,file:/D:/program/openeap_devstudio/workspace/MyTest/bin/

注意:找目录,一般会以斜杠结尾,但是如果这样:Test.class.getResource("/com")这是明确明确不以斜杠结尾,就file:/D:/program/openeap_devstudio/workspace/MyTest/bin/com


注意:如果是web project,那么把上面的
file:/D:/program/openeap_devstudio/workspace/MyTest/bin/
改成file:/D:/program/openeap_devstudio/workspace/MyWebTest/WebRoot/WEB-INF/classes/即可

3。
for(Enumeration e=props.propertyNames(); e.hasMoreElements();){
String key=(String) e.nextElement();
System.out.println(key+"===>"+p.getProperty(key));
}


for(Object t:props.keySet()){
System.out.println(t+"="+p.getProperty(t.toString()));
}
//这时那个value就是afafdafdsf


4.既然可以load当然也可以存储,但是存储的键值对必须是String

props.setProperty("count",String.valueOf(1));//这只是放到了内存中
props.store(new FileOutputStream("count.txt"),"这句话会被写到count.txt的顶上");


下面获取调用的类的路径
Class cs = Reflection.GetCallerClass(2);
System.out.println(caller.getProtectionDomain().getCodeSource().getLocation().getFile());

或者:
StackTraceElement stack[] = (new Throwable()).getStackTrace();


[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值