Introduction of properties File
The properties file in Java is a configuration file, mainly used to express configuration information, the file type is *.properties, the format is a text file, the file content is the format is "key = value" format, in the properties file, you can use "#" to make comments, The properties file is a lot of places to use in Java programming and is easy to operate.
Properties File Example:
# The following is server, database information
Dbport = localhost
DatabaseName = MyDB
Dbusername = root
Dbpassword = root
# The following is the database table information
DBTable = MyTable
# The following is server information
IP = 192.168.0.9
In the above file we assume that the file name is: Test.properties file. Where # begins a behavior annotation information, which we call the key on the left side of the equal sign "=" and what we call value on the right side of the equal sign "=". (In fact, we often say that key-value pairs) key should be the variables in our program. And value is based on the actual situation we configure.
Two, Java common read properties file method
1, using the Java.util.Properties class of the Load () method Example: Java code inputstream in = Lnew bufferedinputstream (New FileInputStream ( name)); Properties P = new properties (); P.load (in);
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >inputstream in = Lnew bufferedinputstream (new FileInputStream (name));
Properties P = new properties ();
P.load (in);</span></span>
2, the use of Java.util.ResourceBundle class Getbundle () method
Example: Java code resourcebundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >resourcebundle RB = Resourcebundle.getbundle (name, Locale.getdefault ()); </span></span>
Using ResourceBundle to read. properties files to avoid path problems
When I read the. properties file in the jar, I always can't find the file path, and later read the. properties file with ResourceBundle to avoid the path problem, the code is as follows:
Process is the filename, remember not to add. Properties, URL is the key name in the file
Java code ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process"); String s = bundle.getstring ("URL"); System.out.println (s); PURL = s;
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" > ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process");
String s = bundle.getstring ("URL");
System.out.println (s);
PURL = s;</span></span>
3, the use of the Java.util.PropertyResourceBundle class of constructors
Example:
Java code InputStream in = new Bufferedinputstream (new FileInputStream (name)); ResourceBundle RB = new propertyResourceBundle (in);
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >inputstream in = new Bufferedinputstream (new FileInputStream (name));
ResourceBundle RB = new propertyResourceBundle (in); </span></span>
4, the use of class variables getResourceAsStream () method
Example:
Java code InputStream in = Class name. Class.getresourceasstream (name); Properties P = new properties (); P.load (in);
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >inputstream in = class name. Class.getresourceasstream (name);
Properties P = new properties ();
P.load (in); </span></span>
5, the use of Class.getclassloader () to obtain the Java.lang.ClassLoader getResourceAsStream () method Example:
Java code InputStream in = Class name. Class.getclassloader (). getResourceAsStream (name); Properties P = new properties (); P.load (in);
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >inputstream in = class name. Class.getclassloader (). getResourceAsStream (name);
Properties P = new properties ();
P.load (in); </span></span>
6. Examples of Getsystemresourceasstream () static methods using the Java.lang.ClassLoader class :
Java code InputStream in = Classloader.getsystemresourceasstream (name); Properties P = new properties (); P.load (in);
<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >inputstream in = Classloader.getsystemresourceasstream (name);
Properties P = new properties ();
P.load (in); </span></span>
7. Examples of getResourceAsStream () methods that can use Javax.servlet.ServletContext in the servlet :
Java code InputStream in = Context.getresourceasstream (path); Properties P = new properties (); P.load (in);
Read the configuration file using ResourceBundle
If I now have a database configuration file that I write as a resource file style, then:
1
2
3
4
5
|
#数据库配置信息
driver=com.mysql.jdbc.driver
url=jdbc:mysql://localhost:3306/cns
user=test
password= Test
|
Next, we use the ResourceBundle class to process:
1 2 3 4 5 6 7 8 9 A ' |
Package Forum;
import java.util.*; Public class Redprofile { public Static
Strong>void Main (string[] a) {String resourcefile = "Forum.cns"; Create a default ResourceBundle object//ResourceBundle Find the Cns.properties file under the Package forum//Forum is the resource's package name, which is the same as the generic The naming rules for Java classes are exactly the same://-case-sensitive//-extension. properties omitted. Just as the class can be omitted. class extension-The resource file must be located under the path of the specified package (in the specified classpath)//If you are using it in a non-Web project, be sure to write the path to the resource file, which is
The package path must exist.
If you are a Web project, you do not write a package path, you can put the resource file in the Web-inf\classes\ directory at this point.
ResourceBundle RB = Resourcebundle.getbundle (resourcefile);
System.out.println (rb.getstring ("DRIVER"))//Here is the case, hey. Output value is Jdbc:mysql://localhost:3306/cns}} |
Can write this reason, look at the following source code
/** * Converts the given <code>bundleName</code> to the form required * by the {@link Classl Oader#getresource Classloader.getresource} * by replacing all occurrences of <code> '. '
</code> in * <code>bundleName</code> with <code> '/' </code> and appending a * <code> '. ' </code> and the given file <code>suffix</code>. For * example, if <code>bundleName</code> are * <code> "Foo.bar.MyResources_ja_JP" </c Ode> and <code>suffix</code> * is <code> "Properties" </CODE> then * <code& gt; "
Foo/bar/myresources_ja_jp.properties "</code> is returned. * * @param bundlename * The bundle name * @param suffix * The file type Suffix * @return The converted resource Name * @exception NullPointerException * If <code>bundlename</code> or <code>suffix</code> * is <code>null</code> */Public final String toresourcename (string bundlename, string suffix) {StringBuilder SB = n
EW StringBuilder (Bundlename.length () + 1 + suffix.length ()); Sb.append (Bundlename.replace ('. ', '/')). Append ('. ').
Append (suffix);
return sb.tostring (); Ultimately, the resource file is loaded with ClassLoader: else if (format.equals ("Java.properties")) {final String resourcename =
TORESOURCENAME0 (Bundlename, "Properties");
if (resourcename = = null) {return bundle;
Final ClassLoader ClassLoader = loader;
Final Boolean reloadflag = reload;
InputStream stream = null; try {stream = accesscontroller.doprivileged (new privilegedexceptionaction<
;inputstream> () { Public InputStream Run () throws IOException {InputStream is =
Null
if (reloadflag) {URL url = classloader.getresource (resourcename); if (URL!= null) {URLConnection connection = Url.openconnection
();
if (connection!= null) {//Disable caches to get fresh data for
Reloading.
Connection.setusecaches (FALSE);
is = Connection.getinputstream (); }} else {is =
Classloader.getresourceasstream (resourcename); return is;
}
});
catch (Privilegedactionexception e) {throw (IOException) e.getexception (); } if (stream!= null) {try {bundle = new Propertyresourcebun
Dle (stream);
finally {stream.close ();
}} else {throw new IllegalArgumentException ("Unknown format:" + format);
And eventually the Java class Properties public propertyResourceBundle (InputStream stream) is used throws IOException {
Properties Properties = new properties ();
Properties.load (stream);
Lookup = new HashMap (properties); }