[英]Thread.currentThread().getContextClassLoader().getResourceAsStream reads a property file multiple times
I have a property file named sysconfig.properties, I want to read it multiple times, because it is mutable.But I found when I changed the content of the sysconfig.properties then I read the content that is imutable, which is the same with the first time I read from the systemconfig.properties file.The content of the sysconfig.propertes file as follows: 我有一个名为sysconfig.properties的属性文件,我想多次读取它,因为它是可变的。但是我发现,当我更改sysconfig.properties的内容时,我会读取不可变的内容,这与第一次读取systemconfig.properties文件。sysconfig.propertes文件的内容如下:
isInitSuccess=TRUE
isStartValid=2013
May be sometime it will been changed as follows: 可能会在某些时候进行如下更改:
isInitSuccess=FALSE
isStartValid=2013
The code of read the properties file as follows: 读取属性文件的代码如下:
InputStream inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
I use the code read the file mutilple times, but every time the "isInitSuccess" is "TRUE", even though I changed the isInitSuccess=FALSE.Is the system just read it one time, then I read the file, it just get the input stream from the memory? 我使用代码多次读取文件,但是每次“ isInitSuccess”为“ TRUE”时,即使我将isInitSuccess = FALSE更改了。系统是否只读取了一次,然后读取了文件,也得到了从内存输入流? But when I use the code below, it will work fine:
但是当我使用下面的代码时,它将正常工作:
InputStream inStream = new FileInputStream(new File(strPath));
I googled, but I did not find any help, the problem confused me a lot, any help would be appreciate. 我用谷歌搜索,但没有找到任何帮助,这个问题使我很困惑,任何帮助将不胜感激。
You need to read up on what the classpath is. 您需要阅读类路径是什么。
In short, Java has a concept of classpath which includes all the resources ( .class
files, .properties
files, and anything really) it needs to run. 简而言之,Java具有类路径的概念,该类路径包括它需要运行的所有资源(
.class
文件, .properties
文件以及所有实际内容)。 When you use ClassLoader#getResourceAsStream(String)
, you're actually getting the InputStream
of a classpath resource. 使用
ClassLoader#getResourceAsStream(String)
,实际上是在获取类路径资源的InputStream
。 This resource can be a physical resource on disk or it can be in an archive. 此资源可以是磁盘上的物理资源,也可以位于存档中。
When you use a FileInputStream
, you are getting the InputStream
of a file on disk. 使用
FileInputStream
,将获取磁盘上文件的InputStream
。
The InputStream
from the ClassLoader
and the one from the FileInputStream
do not correspond to the same file. 来自
ClassLoader
的InputStream
和来自FileInputStream
不对应于同一文件。
You should read up on how your IDE (or whatever build system) handles your files. 您应该阅读有关IDE(或任何构建系统)如何处理文件的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.