你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Web开发
java中getResourceAsStream的问题 - 缓存
 

运行状态下property文件已经被改动了,但是使用getResourceAsStream()方法读取的文件没发生变化,还是和最初启动服务器时的一致。在网上查了一下,有人说是getResourceAsStream()方法读取新文件后会被java虚拟机缓存,而再次调用getResourceAsStream()方法时会先查找java虚拟机中是否有此文件,如果有则直接返回,如果没有才会去根据传入的name获取文件

 


[java]
final Properties prop = new Properties(); 
 
Property(String file){ 
     
    try { 
        prop.load(this.getClass().getResourceAsStream(file)); 
    } catch (FileNotFoundException e) { 
        e.printStackTrace(System.err); 
    } catch (IOException e) { 
        e.printStackTrace(System.err); 
    } 

应该改为

 

[java]
final Properties prop = new Properties(); 
 
Property(String file){ 
     
    try { 
        prop.load(new FileInputStream(this.getClass().getResource(file).getFile())); 
         
    } catch (FileNotFoundException e) { 
        e.printStackTrace(System.err); 
    } catch (IOException e) { 
        e.printStackTrace(System.err); 
    } 

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089