How To Practise Together With Modification Properties File Aeroplane Coffee Computer Program Inwards Text Together With Xml Format
Though most of the fourth dimension nosotros practise too alter properties file using text editor similar notepad, word-pad or edit-plus, It’s likewise possible to practise too edit properties file from Java program. Log4j.properties, which is used to configure Log4J based logging inwards Java too jdbc.properties which is used to specify configuration parameters for database connectivity using JDBC are ii most mutual instance of belongings file inwards Java. Though I accept non constitute whatever existent province of affairs where I require to practise properties file using Java plan simply it’s ever expert to know virtually facilities available inwards Java API. In terminal Java tutorial on Properties nosotros accept seen how to read values from properties file on both text too XML format too inwards this article nosotros volition encounter how to practise properties file on both text too XML format. Java API’s java.util.Properties shape provides several utility store() methods to shop properties inwards either text or xml format. Store() tin flaming live on used to shop belongings inwards text properties file too storeToXML() method tin flaming live on used for creating a Java belongings file inwards XML format.
Java plan to practise too shop Properties inwards text too XML format
As I said before java.util.Properties correspond belongings file inwards Java program. It provides load() too store() method to read too write properties files from too to File system. Here is uncomplicated instance of creating Java belongings file shape plan itself. import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java plan to practise too alter belongings file inwards text format too storing
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java plan to practise too alter belongings file inwards text format too storing
* belongings on it. Most of the fourth dimension nosotros role text editor to practise too edit belongings
* file e.g. jdbc.properties or log4j.properties simply nosotros tin flaming likewise practise belongings
* file from Java plan 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 practise user.properties file inwards C:\. hither is how that belongings file volition await like:
#Properties file generated from Java program
#Mon January sixteen 03:00:57 VET 2012
key2=value2
key1=value1
Here is around 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 plan to shop properties inwards XML belongings file. stroeToXML() method of
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Java plan to shop properties inwards XML belongings file. stroeToXML() method of
* java.util.Properties shape 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 practise Properties file from Java plan 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, y'all tin flaming likewise edit them from Java plan if needed. I personally prefer properties file inwards text format if position out of properties is non much too XML based properties file if file is big plenty e.g. if y'all accept many properties to leverage XML editors.
Further Learning
Java In-Depth: Become a Complete Java Engineer!
Master Java Web Services too REST API amongst Spring Boot
How to read XML files using SAX parser inwards Java
Komentar
Posting Komentar