How to specify the path when the java program reads the resource file?
Java program, there are two ways to read resources.
First, it is directly carried out through File. For example, FileReader and BufferedReader. The file directory is relative to the Project. For example, if I create a new MyProject, I have an icons folder under MyProject. to read the content in icons, the path is like "icons /***. the relative path of gif.
The second is obtained through getClass (). getResource. The second method must ensure that the file is in the path included in the classpath. The general classpath (you can view the. classpath file of any eclipse project) includes at least the following content:
We can see that src and bin are default, so the second method is that the resource files must be in these two folders. Therefore, if there is a property. ini file in the src folder, you can use getResource ("/property. ini"). Note that it must start.
The maven project has a resource folder by default, which is located in the classpath path. Therefore, the resource files are usually put here, And the content can be conveniently obtained through getResource.
Java specially designed a class for processing configuration information such as xxx = xxx, ResourceBundle. The general processing method is as follows,
ResourceBundle resource = ResourceBundle. getBundle ("conf/a.txt ");
PropertyResourceBundle bundle = (PropertyResourceBundle) bundle;
Enumeration Keys = bundle. keys ();
For (String key: keys)
{
// Solve bundle. getString (key)
}