Though most of the fourth dimension nosotros do in addition to alteration properties file using text editor similar notepad, word-pad or edit-plus, It’s likewise 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 holding file inward Java. Though I convey non institute whatever existent province of affairs where I postulate to do properties file using Java programme but it’s ever practiced to know virtually facilities available inward Java API. In terminal Java tutorial on Properties nosotros convey seen how to read values from properties file on both text in addition to XML format in addition to inward this article nosotros volition run into how to do properties file on both text in addition to XML format. Java API’s java.util.Properties course of report provides several utility store() methods to shop properties inward either text or xml format. Store() tin move used to shop holding inward text properties file in addition to storeToXML() method tin move used for creating a Java holding 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 upwardly for holding 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 elementary instance of creating Java holding file cast 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 alteration holding 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 alteration holding file inward text format in addition to storing
* holding on it. Most of the fourth dimension nosotros purpose text editor to do in addition to edit holding
* file e.g. jdbc.properties or log4j.properties but nosotros tin likewise do holding
* file from Java programme equally 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 holding file volition expect like:
#Properties file generated from Java program
#Mon January sixteen 03:00:57 VET 2012
key2=value2
key1=value1
Here is merely about other instance of creating Java holding 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 holding file. stroeToXML() method of
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java programme to shop properties inward XML holding file. stroeToXML() method of
* java.util.Properties course of report is used to salvage properties inward XML holding 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 alteration Properties from Java program. As I said almost all fourth dimension nosotros purpose text editor e.g. notepad or notepad++ to edit properties file similar log4j.properties or jdbc.properties, you lot tin likewise edit them from Java programme if needed. I personally prefer properties file inward text format if give away of properties is non much in addition to XML based properties file if file is big plenty e.g. if you lot convey 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