In Java development, we usually store configuration parameter information to a property file, which can be a property file with key value pairs, or an XML file, and refer to the blog "Java Programming" DOM XML Parser parsing, traversing, and creating XML. In this blog post, I'll show you how to write a key-value pair to a property file, how to read the key-value pairs in the property file, and how to traverse the property file.
1, write the key value to the property file
Special attention:
The properties class calls the SetProperty method to save the key value pair in memory, which can be traversed by the GetProperty method, PropertyNames () method, but does not persist the key value pair to the property file, so it is necessary to call the store () method to persist the key value pair to the property file, the store method here resembles the commit () method of the Android Sharedpreferences.
Package Com.andieguo.propertiesdemo;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.util.Date;
Import java.util.Enumeration;
Import java.util.Properties;
Import Junit.framework.TestCase; public class Propertiestester extends TestCase {public void WriteProperties () {Properties Properties = new Propertie
S ();
OutputStream output = null;
try {output = new FileOutputStream ("Config.properties");
Properties.setproperty ("url", "jdbc:mysql://localhost:3306/");
Properties.setproperty ("username", "root");
Properties.setproperty ("Password", "root"); Properties.setproperty ("Database", "BBS");//Save key value pair to memory Properties.store (output, "Andieguo modify" + New Date (). toString
());//Save key value pair to file for catch (IOException io) {io.printstacktrace ();
Finally {if (output!= null) {try {output.close (); catch (IOException e) {E.printstacktracE ();
}
}
}
}
}After the unit tests are performed, the properties file contents are as follows:
2. Read the key value pairs in the property file
public class Propertiestester extends TestCase {public
void Loadproperties () {
Properties properties = New Proper Ties ();
InputStream input = null;
try {
input = new FileInputStream ("config.properties");//load the configuration file Properties.load (input) Under the Java Project root path;//
Load Property file
System.out.println ("url:" + properties.getproperty ("url"));
System.out.println ("Username:" + properties.getproperty ("username"));
System.out.println ("Password:" + properties.getproperty ("password"));
SYSTEM.OUT.PRINTLN ("Database:" + properties.getproperty ("database"));
} catch (IOException io) {
} finally {
if (input!= null) {
try {
input.close ();
} catch (Ioexcepti On e) {
e.printstacktrace ();}}}
To perform the unit test method, the output of the console outputs is as follows:
3, traversing the property file in the key value pairs
Package Com.andieguo.propertiesdemo;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.Enumeration;
Import Java.util.Map.Entry;
Import java.util.Properties;
Import Java.util.Set;
Import Junit.framework.TestCase;
public class Propertiestester extends TestCase {public void printall () {Properties prop = new properties ();
InputStream input = null;
try {String filename = ' config.properties ';
input = GetClass (). getClassLoader (). getResourceAsStream (filename);
if (input = = null) {System.out.println ("Sorry, unable to find" + filename);
Return
} prop.load (input); Method One: Set<object> keys = Prop.keyset ()//Returns the collection for the property key for (Object Key:keys) {System.out.println ("key:" +key.t
Ostring () + ", Value:" +prop.get (key); }//Method Two: Set<entry<object, object>> entrys = Prop.entryset ();//Return property key value to entity for (Entry<object, Object > Entry:entrys) {System.out.println ("key:" +entry.getkey () + ", Value:" +entry.getvalue ());
//Method III: Enumeration<?> e = Prop.propertynames ();
while (E.hasmoreelements ()) {string key = (String) e.nextelement ();
String value = Prop.getproperty (key);
System.out.println ("key:" + key + ", Value:" + value);
} catch (IOException ex) {ex.printstacktrace ();
finally {if (input!= null) {try {input.close ();
catch (IOException e) {e.printstacktrace ();
}
}
}
}
}4. Other Methods
Public void List (PrintStream out)
Prints the list of attributes to the specified output stream. This method is useful for debugging.
Public void Storetoxml (OutputStream os,stringcomment) throws IOException
Emits an XML document that represents all the attributes contained in this table. 5, reference
Java Properties File Examples (recommended)
Java Property File example with write, read, load from Classpath and property XML file
Java-the Properties Class 6, articles that you may be interested in
Java Programming DOM XML Parser parsing, traversing, creating XML
"Java Programming" SAX XML parser parsing, generating XML files