Properties file is a simple text file with extension .properties generally used to store projects configurations like database connection details and text labels in case project is a web application.
Here is a sample to read data from a properties file and then write it back to an xml file.
I have a properties file named test.properties with one entry as below.
// test.properties
msg1=hello bharat, this is comming from properties file.
// test.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>my test comment</comment>
<entry key="msg2">This was written to properties file from java</entry>
<entry key="msg1">hello bharat, this is comming from properties file.</entry>
</properties>
I prefer storing ddl sql statements in a xml properties file as queries are formatted and readable.
Here is a sample to read data from a properties file and then write it back to an xml file.
I have a properties file named test.properties with one entry as below.
// test.properties
msg1=hello bharat, this is comming from properties file.
package com.bharat.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.util.Properties;
public class ReadWritePropertiesFile
{
public static void main(String... args) throws Exception
{
Properties props = new Properties();
props.load(new FileReader("test.properties"));
System.out.println(props.get("msg1"));
props.put("msg2", "This was written to properties file from java");
OutputStream os = new FileOutputStream(new File("test.xml"));
props.storeToXML(os, "my test comment");
}
}
This is the xml file where properties get written to.
// test.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>my test comment</comment>
<entry key="msg2">This was written to properties file from java</entry>
<entry key="msg1">hello bharat, this is comming from properties file.</entry>
</properties>
I prefer storing ddl sql statements in a xml properties file as queries are formatted and readable.
No comments:
Post a Comment