We typically store configuration parameters for Java applications in the properties file. The Java application's properties file can be a normal key-value-based file with the properties extension. can also be an XML file.
In this case. We will show you how to output the properties files in both formats through the Java program. It also describes how to load and use these two properties files from Classpath.
The following is the case program code:
Propertyfilesutil.java
PackageCom.journaldev.util;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.util.Properties;ImportJava.util.Set; Public class propertyfilesutil { Public Static void Main(string[] args)throwsIOException {String Propertyfilename ="Db.properties"; String XMLfileName ="Db.xml"; Writepropertyfile (Propertyfilename, XMLfileName); Readpropertyfile (Propertyfilename, XMLfileName); Readallkeys (Propertyfilename, XMLfileName); Readpropertyfilefromclasspath (Propertyfilename); }/** * Read Property file from Classpath * @param propertyfilename * @throws IOException */ Private Static void Readpropertyfilefromclasspath(String propertyfilename)throwsIOException {Properties prop =NewProperties (); Prop.load (PropertyFilesUtil.class.getClassLoader (). getResourceAsStream (Propertyfilename)); System.out.println (Propertyfilename +"loaded from Classpath::d b.host ="+prop.getproperty ("Db.host")); System.out.println (Propertyfilename +"loaded from Classpath::d b.user ="+prop.getproperty ("Db.user")); System.out.println (Propertyfilename +"loaded from Classpath::d b.pwd ="+prop.getproperty ("Db.pwd")); System.out.println (Propertyfilename +"loaded from classpath::xyz ="+prop.getproperty ("XYZ")); }/** * Read all the keys from the given property files * @param propertyfilename * @param XMLfileName * @throws ioexception * * Private Static void Readallkeys(String propertyfilename, String xmlfilename)throwsIOException {System.out.println ("Start of Readallkeys"); Properties prop =NewProperties (); FileReader reader =NewFileReader (Propertyfilename); Prop.load (reader); Set<object> keys= Prop.keyset (); for(Object Obj:keys) {System.out.println (Propertyfilename +":: key="+obj.tostring () +":: Value="+prop.getproperty (Obj.tostring ())); }//loading XML file Now, first clear existing propertiesProp.clear (); InputStream is =NewFileInputStream (XMLfileName); Prop.loadfromxml (IS); keys= Prop.keyset (); for(Object Obj:keys) {System.out.println (XMLfileName +":: key="+obj.tostring () +":: Value="+prop.getproperty (Obj.tostring ())); }//now Free all the resourcesIs.close (); Reader.close (); System.out.println ("End of Readallkeys"); }/** * This method reads the files from file system * @param propertyfilename * @param XMLfileName * @throws ioexception * @throws filenotfoundexception * * Private Static void Readpropertyfile(String propertyfilename, String xmlfilename)throwsFileNotFoundException, IOException {System.out.println ("Start of Readpropertyfile"); Properties prop =NewProperties (); FileReader reader =NewFileReader (Propertyfilename); Prop.load (reader); System.out.println (Propertyfilename +"::d b.host ="+prop.getproperty ("Db.host")); System.out.println (Propertyfilename +"::d b.user ="+prop.getproperty ("Db.user")); System.out.println (Propertyfilename +"::d b.pwd ="+prop.getproperty ("Db.pwd")); System.out.println (Propertyfilename +":: XYZ ="+prop.getproperty ("XYZ"));//loading XML file Now, first clear existing propertiesProp.clear (); InputStream is =NewFileInputStream (XMLfileName); Prop.loadfromxml (IS); System.out.println (XMLfileName +"::d b.host ="+prop.getproperty ("Db.host")); System.out.println (XMLfileName +"::d b.user ="+prop.getproperty ("Db.user")); System.out.println (XMLfileName +"::d b.pwd ="+prop.getproperty ("Db.pwd")); System.out.println (XMLfileName +":: XYZ ="+prop.getproperty ("XYZ"));//now Free all the resourcesIs.close (); Reader.close (); System.out.println ("End of Readpropertyfile"); }/** * This method writes the property files to file system in property file * and XML format * @param fileName * @throws ioexception * * Private Static void Writepropertyfile(String propertyfilename, String xmlfilename)throwsIOException {System.out.println ("Start of Writepropertyfile"); Properties prop =NewProperties (); Prop.setproperty ("Db.host","localhost"); Prop.setproperty ("Db.user","User"); Prop.setproperty ("Db.pwd","Password"); Prop.store (NewFileWriter (Propertyfilename),"DB Config file"); System.out.println (Propertyfilename +"written successfully"); Prop.storetoxml (NewFileOutputStream (XMLfileName),"DB Config XML file"); System.out.println (XMLfileName +"written successfully"); System.out.println ("End of Writepropertyfile"); }}
When you execute this code. The Writepropertyfile method generates a property file in both of these formats and stores the file under Project's root folder.
The contents of the two properties files generated by the Writepropertyfile method are:
Db.properties
#DB Config file#Fri Nov 16 11:16:37 PST 2012db.user=userdb.host=localhostdb.pwd=password
Db.xml
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?><! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" ><properties><comment>DB Config XML File</Comment><entry key="Db.user">User</Entry><entry key="Db.host">localhost</Entry><entry key="Db.pwd">Password</Entry></Properties>
It is important to note that the comment element, prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
when we use this code, the second argument comes into the gaze content, assuming that null is passed in, and the resulting XML property file will have no comment element.
Console output such as the following:
Start of Writepropertyfiledb. PropertiesWritten Successfullydb. XMLWritten successfullyend of Writepropertyfilestart of Readpropertyfiledb.properties::d b. Host= localhostdb.properties::d b. User= Userdb.properties::d b. PWD= passworddb.properties:: XYZ = nullDb.xml::d b. Host= localhostDb.xml::d b. User= UserDb.xml::d b. PWD= passwordDb.xml:: XYZ = Nullend of Readpropertyfilestart of Readallkeysdb.properties:: key=db. User:: Value=userdb.properties:: key=db. Host:: Value=localhostdb.properties:: key=db. PWD:: Value=passwordDb.xml:: key=db. User:: Value=userDb.xml:: key=db. Host:: Value=localhostDb.xml:: key=db. PWD:: Value=passwordend of ReadallkeysexceptioninchThread"Main"Java. Lang. NullPointerExceptionAt Java. Util. Properties$LineReader. ReadLine(Properties. Java:434) at Java. Util. Properties. Load0 (Properties. Java:353) at Java. Util. Properties. Load(Properties. Java:341) atcom. Journaldev. Util. Propertyfilesutil. Readpropertyfilefromclasspath(Propertyfilesutil. Java: