tags: Java virtual machine Web Blog
There was a problem with the project using the getResourceAsStream() method:
The property file has been changed while the project is running, but the file read using the getResourceAsStream() method has not changed, which is the same as when the server was originally started. Checked on the Internet, some people say that the getResourceAsStream () method will be cached by the java virtual machine after reading the new file, and when the getResourceAsStream () method is called again, it will first find whether there is this file in the java virtual machine, if there is, then directly return If not, it will get the file according to the incoming name.
Start the code I wrote:
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);
}
}
Changed code:
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);
}
}
This way, there will be no caching issues.
Need to explain is that I am a java web project, so I need to passThis.getClass().getResource(file).getFile() code to get the path.
The file name parameter file passed in can be a path such as "/binessID.properties".
Add "/" to find the file from the classpath, without adding "/" is to find from the current class class directory.
This can be referenced
The method uses a ClassLoader getResource utf-8 encoded path information, when the pathThe presence of Chinese and space, he will convert these characters, so that, more often than not get what we wan...
Java uses getResource and getResourceAsStream to get the path of the file is based on the following project: the src directory is set to source root and the resources directory is set to resources roo...
Prior to want to get a resource file to do some processing, use getClass (). GetResourceAsStream () has been unable to get the file. Specific usage. Specific file and location of the code is in src...
Although there are numerous differences between the getResource () and getResourceAsStream () methods of ClassLoader and Class on the Internet, but I also encountered this problem, so I recorded it. &...
I have seen this method in the video tutorial of JAVA connection to the database. I found it on the Internet if I thought it should be important. Write it down now that you have learned it, and forget...
Directory Structure: debug finds that in is empty, the code is not wrong, only the content of the db.properties file is not read The first feeling is that there may be a problem with the resources fil...
Foreword: Many times we need to load the configuration information in the properties file under the resources directory, but the path problem is always a headache. Today, I will specially summarize it...
Article Directory 1. Class's getResource and getResourceAsStream 2. ClassLoad's getResource and getResourceAsStream 3. Suggestions Project source file structure The structure of the project after comp...
Directly on the map, to facilitate comparison I do not understand even a path in which the above configuration, oh, on the map, even though some of my colleagues in the eyes of eclise have become anti...
I wanted to implement music loop playback in a java application, but the return value of getResource("music/bg.wav") is null. I guess the path is a problem. I tried tryResource("/music/...