How To Occupation As Well As Alteration Properties File Degree Coffee Plan Inwards Text As Well As Xml Format
Though most of the fourth dimension nosotros do in addition to alter properties file using text editor similar notepad, word-pad or edit-plus, It’s every bit good possible to do in addition to edit properties file from Java program. Log4j.properties, which is used to configure Log4J based logging inward Java in addition to jdbc.properties which is used to specify configuration parameters for database connectivity using JDBC are ii most mutual instance of belongings file inward Java. Though I accept non constitute whatever existent province of affairs where I postulate to do properties file using Java programme merely it’s ever practiced to know nearly facilities available inward Java API. In concluding Java tutorial on Properties nosotros accept seen how to read values from properties file on both text in addition to XML format in addition to inward this article nosotros volition encounter how to do properties file on both text in addition to XML format. Java API’s java.util.Properties degree provides several utility store() methods to shop properties inward either text or xml format. Store() tin hand notice hold upward used to shop belongings inward text properties file in addition to storeToXML() method tin hand notice hold upward used for creating a Java belongings file inward XML format.
Java programme to do in addition to shop Properties inward text in addition to XML format
As I said before java.util.Properties stand upward for belongings file inward Java program. It provides load() in addition to store() method to read in addition to write properties files from in addition to 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 do in addition to alter belongings file inward text format in addition to storing
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java programme to do in addition to alter belongings file inward text format in addition to storing
* belongings on it. Most of the fourth dimension nosotros run text editor to do in addition to edit belongings
* file e.g. jdbc.properties or log4j.properties merely nosotros tin hand notice every bit good do belongings
* file from Java programme every bit shown inward 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 do user.properties file inward 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 inward 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 inward XML belongings file. stroeToXML() method of
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java programme to shop properties inward XML belongings file. stroeToXML() method of
* java.util.Properties degree is used to relieve properties inward XML belongings file from Java
* program.
*
* @author Javin Paul
*/
public class XmlPropertiesWriter {
public static void main(String args[]) throws FileNotFoundException, IOException {
//Reading properties files inward 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 inward 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 inward 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 inward 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 inward 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 inward xml format generated from Java program</comment>
<entry key="key2">value2</entry>
<entry key="key1">value1</entry>
</properties>
That’s all on How to do Properties file from Java programme or How to alter Properties from Java program. As I said almost all fourth dimension nosotros run text editor e.g. notepad or notepad++ to edit properties file similar log4j.properties or jdbc.properties, yous tin hand notice every bit good edit them from Java programme if needed. I personally prefer properties file inward text format if set out of properties is non much in addition to XML based properties file if file is big plenty e.g. if yous accept many properties to leverage XML editors.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services in addition to REST API amongst Spring Boot
How to read XML files using SAX parser inward Java
0 Response to "How To Occupation As Well As Alteration Properties File Degree Coffee Plan Inwards Text As Well As Xml Format"
Post a Comment