How To Practise Too Alter Properties File Course Of Report Coffee Plan Inward Text Too Xml Format
Wednesday, June 13, 2018
Add Comment
Though most of the fourth dimension nosotros exercise together with alter properties file using text editor similar notepad, word-pad or edit-plus, It’s besides possible to exercise together with edit properties file from Java program. Log4j.properties, which is used to configure Log4J based logging inwards Java together with jdbc.properties which is used to specify configuration parameters for database connectivity using JDBC are 2 most mutual instance of belongings file inwards Java. Though I bring non institute whatever existent province of affairs where I involve to exercise properties file using Java programme merely it’s e'er skilful to know most facilities available inwards Java API. In terminal Java tutorial on Properties nosotros bring seen how to read values from properties file on both text together with XML format together with inwards this article nosotros volition encounter how to exercise properties file on both text together with XML format. Java API’s java.util.Properties course of teaching provides several utility store() methods to shop properties inwards either text or xml format. Store() tin post away hold upwards used to shop belongings inwards text properties file together with storeToXML() method tin post away hold upwards used for creating a Java belongings file inwards XML format.
Java programme to exercise together with shop Properties inwards text together with XML format
As I said before java.util.Properties correspond belongings file inwards Java program. It provides load() together with store() method to read together with write properties files from together with to File system. Here is unproblematic instance of creating Java belongings file shape programme itself.
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java programme to exercise together with alter belongings file inwards text format together with storing
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java programme to exercise together with alter belongings file inwards text format together with storing
* belongings on it. Most of the fourth dimension nosotros role text editor to exercise together with edit belongings
* file e.g. jdbc.properties or log4j.properties merely nosotros tin post away besides exercise belongings
* file from Java programme every bit shown inwards this example.
*
* @author Javin Paul
*/
public class TextPropertyWriter {
public static void main(String args[]) throws FileNotFoundException, IOException {
//Creating properties files from Java program
Properties props = new Properties();
FileOutputStream fos = new FileOutputStream("c:/user.properties");
props.setProperty("key1", "value1");
props.setProperty("key2", "value2");
//writing properites into properties file from Java
props.store(fos, "Properties file generated from Java program");
fos.close();
}
}
* @author Javin Paul
*/
public class TextPropertyWriter {
public static void main(String args[]) throws FileNotFoundException, IOException {
//Creating properties files from Java program
Properties props = new Properties();
FileOutputStream fos = new FileOutputStream("c:/user.properties");
props.setProperty("key1", "value1");
props.setProperty("key2", "value2");
//writing properites into properties file from Java
props.store(fos, "Properties file generated from Java program");
fos.close();
}
}
This volition exercise user.properties file inwards C:\. hither is how that belongings file volition hold off like:
#Properties file generated from Java program
#Mon January xvi 03:00:57 VET 2012
key2=value2
key1=value1
Here is roughly other instance of creating Java belongings file inwards XML format from Java program:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java programme to shop properties inwards XML belongings file. stroeToXML() method of
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java programme to shop properties inwards XML belongings file. stroeToXML() method of
* java.util.Properties course of teaching is used to relieve properties inwards XML belongings file from Java
* program.
*
* @author Javin Paul
*/
public class XmlPropertiesWriter {
public static void main(String args[]) throws FileNotFoundException, IOException {
//Reading properties files inwards Java example
Properties props = new Properties();
FileOutputStream fos = new FileOutputStream("c:/user.xml");
props.setProperty("key1", "value1");
props.setProperty("key2", "value2");
//writing properites into properties file from Java
props.storeToXML(fos, "Properties file inwards xml format generated from Java program");
fos.close();
}
}
* @author Javin Paul
*/
public class XmlPropertiesWriter {
public static void main(String args[]) throws FileNotFoundException, IOException {
//Reading properties files inwards Java example
Properties props = new Properties();
FileOutputStream fos = new FileOutputStream("c:/user.xml");
props.setProperty("key1", "value1");
props.setProperty("key2", "value2");
//writing properites into properties file from Java
props.storeToXML(fos, "Properties file inwards xml format generated from Java program");
fos.close();
}
}
Output:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties file inwards xml format generated from Java program</comment>
<entry key="key2">value2</entry>
<entry key="key1">value1</entry>
</properties>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties file inwards xml format generated from Java program</comment>
<entry key="key2">value2</entry>
<entry key="key1">value1</entry>
</properties>
That’s all on How to exercise Properties file from Java programme or How to alter Properties from Java program. As I said almost all fourth dimension nosotros role text editor e.g. notepad or notepad++ to edit properties file similar log4j.properties or jdbc.properties, yous tin post away besides edit them from Java programme if needed. I personally prefer properties file inwards text format if seat out of properties is non much together with XML based properties file if file is big plenty e.g. if yous bring many properties to leverage XML editors.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services together with REST API amongst Spring Boot
How to read XML files using SAX parser inwards Java
0 Response to "How To Practise Too Alter Properties File Course Of Report Coffee Plan Inward Text Too Xml Format"
Post a Comment