We usually save the configuration parameters of Java applications in properties files. The properties files of Java applications can be a normal file based on key-value pairs with properties as the extension, or it can be an XML file.
In this case, we will introduce how to output property files in these two formats through Java programs, and how to load and use these two property files from the classpath.
The following is the case program code:
PropertyFilesUtil.java
package com.journaldev.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Set; public class PropertyFilesUtil { public static void main(String[] args) throws IOException { 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) throws IOException { Properties prop = new Properties(); prop.load(PropertyFilesUtil.class.getClassLoader().getResourceAsStream(propertyFileName)); System.out.println(propertyFileName +" loaded from Classpath::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +" loaded from Classpath::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +" loaded from Classpath::db.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) throws IOException { System.out.println("Start of readAllKeys"); Properties prop = new Properties(); FileReader reader = new FileReader(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 properties prop.clear(); InputStream is = new FileInputStream(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 resources is.close(); reader.close(); System.out.println("End of readAllKeys"); } /** * This method reads property files from file system * @param propertyFileName * @param xmlFileName * @throws IOException * @throws FileNotFoundException */ private static void readPropertyFile(String propertyFileName, String xmlFileName) throws FileNotFoundException, IOException { System.out.println("Start of readPropertyFile"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); System.out.println(propertyFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +"::XYZ = "+prop.getProperty("XYZ")); //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); System.out.println(xmlFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(xmlFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(xmlFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(xmlFileName +"::XYZ = "+prop.getProperty("XYZ")); //Now free all the resources is.close(); reader.close(); System.out.println("End of readPropertyFile"); } /** * This method writes Property files into file system in property file * and xml format * @param fileName * @throws IOException */ private static void writePropertyFile(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of writePropertyFile"); Properties prop = new Properties(); prop.setProperty("db.host", "localhost"); prop.setProperty("db.user", "user"); prop.setProperty("db.pwd", "password"); prop.store(new FileWriter(propertyFileName), "DB Config file"); System.out.println(propertyFileName + " written successfully"); prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file"); System.out.println(xmlFileName + " written successfully"); System.out.println("End of writePropertyFile"); } }
When this code is run, the writePropertyFile method will generate property files in the above two formats and store the files in the root of the project Under contents. The contents of the two property files generated by the
writePropertyFile method:
DB.properties
#DB Config file#Fri Nov 16 11:16:37 PST 2012db.user=user db.host=localhost db.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 should be noted that the comment element, we are using prop .storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
The second parameter in this code is the comment content. If null is passed in, the generated xml attribute file will have no comment element.
The console output is as follows:
Start of writePropertyFile DB.properties written successfully DB.xml written successfully End of writePropertyFile Start of readPropertyFileDB.properties::db.host = localhostDB.properties::db.user = userDB.properties::db.pwd = passwordDB.properties::XYZ = nullDB.xml::db.host = localhostDB.xml::db.user = userDB.xml::db.pwd = passwordDB.xml::XYZ = null End of readPropertyFile Start 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=password End of readAllKeys Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.journaldev.util.PropertyFilesUtil.readPropertyFileFromClasspath(PropertyFilesUtil.java:31) at com.journaldev.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21)
A null pointer exception is reported here. The reason is that the generated file is saved under the root directory of the project, and when reading, it is read from the classpath, and the above generated file is Copy the two property files to src and run the program again.
We usually save the configuration parameters of Java applications in properties files. The properties files of Java applications can be a normal file based on key-value pairs with properties as the extension, or it can be an XML file. .
In this case, we will introduce how to output property files in these two formats through a Java program, and how to load and use these two property files from the classpath.
The following is the case program code:
PropertyFilesUtil.java
package com.journaldev.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Set; public class PropertyFilesUtil { public static void main(String[] args) throws IOException { 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) throws IOException { Properties prop = new Properties(); prop.load(PropertyFilesUtil.class.getClassLoader().getResourceAsStream(propertyFileName)); System.out.println(propertyFileName +" loaded from Classpath::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +" loaded from Classpath::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +" loaded from Classpath::db.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) throws IOException { System.out.println("Start of readAllKeys"); Properties prop = new Properties(); FileReader reader = new FileReader(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 properties prop.clear(); InputStream is = new FileInputStream(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 resources is.close(); reader.close(); System.out.println("End of readAllKeys"); } /** * This method reads property files from file system * @param propertyFileName * @param xmlFileName * @throws IOException * @throws FileNotFoundException */ private static void readPropertyFile(String propertyFileName, String xmlFileName) throws FileNotFoundException, IOException { System.out.println("Start of readPropertyFile"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); System.out.println(propertyFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +"::XYZ = "+prop.getProperty("XYZ")); //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); System.out.println(xmlFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(xmlFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(xmlFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(xmlFileName +"::XYZ = "+prop.getProperty("XYZ")); //Now free all the resources is.close(); reader.close(); System.out.println("End of readPropertyFile"); } /** * This method writes Property files into file system in property file * and xml format * @param fileName * @throws IOException */ private static void writePropertyFile(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of writePropertyFile"); Properties prop = new Properties(); prop.setProperty("db.host", "localhost"); prop.setProperty("db.user", "user"); prop.setProperty("db.pwd", "password"); prop.store(new FileWriter(propertyFileName), "DB Config file"); System.out.println(propertyFileName + " written successfully"); prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file"); System.out.println(xmlFileName + " written successfully"); System.out.println("End of writePropertyFile"); } }
When this code is run, the writePropertyFile method will generate property files in the above two formats and store the files in the root of the project Under contents. The contents of the two property files generated by the
writePropertyFile method:
DB.properties
#DB Config file#Fri Nov 16 11:16:37 PST 2012db.user=user db.host=localhost db.pwd=password
DB.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM " <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 should be noted that the comment element, we are using prop .storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file");
The second parameter in this code is the comment content. If null is passed in, the generated xml attribute file will have no comment element.
The console output content is as follows:
Start of writePropertyFile DB.properties written successfully DB.xml written successfully End of writePropertyFile Start of readPropertyFileDB.properties::db.host = localhostDB.properties::db.user = userDB.properties::db.pwd = passwordDB.properties::XYZ = nullDB.xml::db.host = localhostDB.xml::db.user = userDB.xml::db.pwd = passwordDB.xml::XYZ = null End of readPropertyFile Start 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=password End of readAllKeys Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.journaldev.util.PropertyFilesUtil.readPropertyFileFromClasspath(PropertyFilesUtil.java:31) at com.journaldev.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21)
The above is the content of Java&Xml Tutorial (10) XML as a property file. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!