How to read the properties configuration file in the Web-inf directory in Java code, a solution is given below.
We are accustomed to write some configuration information in the configuration file, such as the database configuration information URL, user and password in the configuration file, so that when the system is deployed, do not need to modify the code, but only need to modify the configuration file.
I put the configuration file in the Web-inf directory under the MyEclipse Engineering folder, and the code to read the configuration file in Java code is this:
String Path = ParametersReader.class.getResource ("/"). GetPath ();
String Websiteurl = (Path.replace ("/build/classes", ""). Replace ("%20", ""). Replace ("classes/", "") + " Parameter.properties "). Replacefirst ("/"," ");
The Parametersreader.class in the first line of code refers to the name of the current class, where the path variable is actually where the current class resides.
In this way, we get the directory of the current project Web-inf. We write the configuration information of the database in the Parameter.properties file, which is placed in the Web-inf directory, which reads as follows:
#JDBC MySQL Connection
mysql_url=jdbc:mysql://127.0.0.1:3306/mydatabasename
mysql_user=root
mysql_ password=123456
So for the above configuration file, the complete code to read the profile should be this:
public static void GetParameters () {
String path = ParametersReader.class.getResource ("/"). GetPath ();
String Websiteurl = (Path.replace ("/build/classes", ""). Replace ("%20", ""). Replace ("classes/", "") + " Parameter.properties "). Replacefirst ("/"," ");
Properties Properties = new properties ();
FileInputStream fileinputstream = null;
try {
FileInputStream = new FileInputStream (websiteurl);
Properties.load (FileInputStream);
Mysql_url = Properties.getproperty ("Mysql_url");
Mysql_user = Properties.getproperty ("Mysql_user");
Mysql_password = Properties.getproperty ("Mysql_password");
} catch (Exception e) {
e.printstacktrace ();
} finally {
try {
fileinputstream.close ();
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
}
}
}
Mysql_url,mysql_user,mysql_password the three variables in the code are defined static variables, and the GetParameters () method is used to assign values to these three variables.
Now more and more use XML file instead of properties file to store configuration information, so we can also put the XML file in the Web-inf directory, through the dom4j and other tools to read the XML file.
Updated----------------------------------------------------2015.12.16------------------------------------------------------
managing configuration files with the class loader
Method One:
Using the ClassLoader class loader, you need to write the package name path for the configuration file, with no slash required at the front.
InputStream is = ReflectTest2.class.getClassLoader (). getResourceAsStream ("cn/itcast/day1/config.properties");
Method Two:
InputStream is = ReflectTest2.class.getResourceAsStream ("config.properties");
If the configuration file and ReflectTest2 are under the same package, write the configuration file name directly;
If the profile is under another package, you need to write an absolute path, and the path needs to be preceded with a slash.