This article illustrates how Java reads a properties file. Share to everyone for your reference. The implementation method is as follows:
Package Com.test.demo;
Import java.util.Properties;
Import Java.io.InputStream;
Import java.io.IOException; /** * Example of read properties file * File:TestProperties.java/Public final class Testproperties {private static St
Ring param1;
private static String param2;
private static String param3;
private static String param4; /** * For the path name of the above configuration file, there is an easy to ignore problem, * that is when you use Object.class.getClassLoader ().
, * can be no need to add "/", * but not getclassloader (). When is not possible, what is the reason?
* Since this configuration file is placed under the SRC of the project, add "/" when the object is loaded.
* If you copy this profile to the same package as the class, you do not need to add, * if you are reading the configuration file in the following way: * * Static {Properties prop = new properties ();
InputStream in = Object.class.getResourceAsStream ("/test.properties");
Documents under src//inputstream in = TestProperties.class.getClassLoader (). getResourceAsStream ("jdbc.properties");
Documents under src InputStream in = TestProperties.class.getResourceAsStream ("jdbc.properties");
The file is under the same package try { Prop.load (in);
param1 = Prop.getproperty ("Mysql.driverclassname"). Trim ();
param2 = Prop.getproperty ("Mysql.url"). Trim ();
PARAM3 = Prop.getproperty ("Mysql.username"). Trim ();
PARAM4 = Prop.getproperty ("Mysql.password"). Trim ();
catch (IOException e) {e.printstacktrace (); }/** * Private constructor method, do not need to create object */Private Testproperties () {} public static String getParam1
() {return param1;
public static String GetParam2 () {return param2;
public static String GetParam3 () {return param3;
public static String GetParam4 () {return param4;
public static void Main (String args[]) {System.out.println (getParam1 ());
System.out.println (GetParam2 ());
System.out.println (GETPARAM3 ());
System.out.println (GETPARAM4 ()); }
}
I hope this article will help you with your Java programming.