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

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)