简体   繁体   English

Thread.currentThread()。getContextClassLoader()。getResourceAsStream多次读取属性文件

[英]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. 来自ClassLoaderInputStream和来自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.

相关问题 Thread.currentThread().getContextClassLoader().getResourceAsStream(); 返回空值 - Thread.currentThread().getContextClassLoader().getResourceAsStream(); return null 了解Thread.currentThread()。getContextClassLoader()。getResourceAsStream() - Understanding Thread.currentThread().getContextClassLoader().getResourceAsStream() Thread.currentThread()。getContextClassLoader()返回多个对象实例 - Thread.currentThread().getContextClassLoader() returns multiple object instances Thread.currentThread()。getContextClassLoader.getResource()中的文件长度不同 - File length is different in Thread.currentThread().getContextClassLoader.getResource() 何时在Web应用程序中使用Thread.currentThread()。getContextClassLoader() - When to use Thread.currentThread().getContextClassLoader() in webapplications 在测试初始化​​方法中模拟Thread.currentThread()。getContextClassLoader() - Mock Thread.currentThread().getContextClassLoader() in test init method Thread.currentThread()。getContextClassLoader()。getResource(“。”)在windows和linux上有不同的结果 - Thread.currentThread().getContextClassLoader().getResource(“.”) has different result on windows and linux 当Thread.currentThread()。getContextClassLoader()返回null时,会发生什么? - When Thread.currentThread().getContextClassLoader() returns null, what is going on? Java Class.forName()vs Thread.currentThread()。getContextClassLoader()。loadClass() - Java Class.forName() vs Thread.currentThread().getContextClassLoader().loadClass() getResourceAsStream 在 Thread.currentThread.getContextClassLoader().getClass() 下返回 null - getResourceAsStream returns null under Thread.currentThread.getContextClassLoader().getClass()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM