How to read and write key/value pairs inside .ini file JAVA (XML store)


Friends the following udemy course created by me;

      Click here to attend Spring Framework 4.x and certification course with a discount

Spring Framework and Core Spring Certification Udemy course with discount
Spring Framework and Core Spring Certification Udemy course with a discount coupon

Use the following code to store key and values inside a .ini file using Java Environment.

This program demonstrates the use of .ini file by implementing regular txt and XML storing methods.

To use the .ini file in this program you should create a folder called ini under the root folder of your project and you should put a file called example.ini under this folder.

/*
 * How to read and write value for an .ini
 * file on Java Environment Example
 */
package inifilereaderwriterexample;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author tunatore
 */
public class INIFileReaderWriterExample {

public static void main(String[] args) {

//appending to the file
//appending file functionality can be modified by changing true and false parameter of
//FileOutputStream object if the parameter is set to false
//it will remove all content of ini file
//of it is set to true it will append the new key/value pairs
INIFileReaderWriterExample.writeKeyValuePairsINIFile("java", "James Gosling");
INIFileReaderWriterExample.writeKeyValuePairsINIFile("linux", "Linus Torvalds");
INIFileReaderWriterExample.writeKeyValuePairsINIFile("microsoft", "Bill Gates");

INIFileReaderWriterExample.readINIFile();

Map<String, String> keyValueMapXML = new HashMap<String, String>();
keyValueMapXML.put("book1", "How to program Java");
keyValueMapXML.put("book2", "PL/SQL programming best practices");
keyValueMapXML.put("book3", "ORACLE Performance Tuning");

//storing key values a XML inside .ini file
INIFileReaderWriterExample.writeKeyValuePairsINIFileXML(keyValueMapXML);

//reading the XML key value pairs inside .ini file
INIFileReaderWriterExample.readINIFileXML();
}
private static void readINIFile() {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir")+"\\ini\\"+"example.ini"));
System.out.println("access with the key java = >> " + props.getProperty("java"));
System.out.println("access with the key linux = >> " + props.getProperty("linux"));
System.out.println("access with the key microsoft = >> " + props.getProperty("microsoft"));
props.list(System.out);

} catch (Exception ex) {
ex.printStackTrace();
}

}

private static void readINIFileXML() {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir")+"\\ini\\"+"example.ini"));
props.loadFromXML(new FileInputStream(System.getProperty("user.dir")+"\\ini\\"+"example.ini"));
System.out.println(props.getProperty("book3"));
System.out.println(props.getProperty("book2"));
System.out.println(props.getProperty("book1"));
} catch (Exception ex) {
ex.printStackTrace();
}

}

private static void writeKeyValuePairsINIFile(String key,String value) {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir") + "\\ini\\" + "example.ini"));
props.setProperty(key, value);
props.store(new FileOutputStream(System.getProperty("user.dir")
+ "\\ini\\" + "example.ini", true), "Here you can enter comment");
} catch (Exception ex) {
ex.printStackTrace();
}

}
private static void writeKeyValuePairsINIFileXML (Map<String, String> keyValuePairs) {

try {
Properties props = new Properties();
//loading the .ini file under ini folder
props.load(new FileInputStream(System.getProperty("user.dir") + "\\ini\\" + "example.ini"));
Iterator mapInterator = keyValuePairs.entrySet().iterator();
while (mapInterator.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry) mapInterator.next();
props.setProperty(pairs.getKey(), pairs.getValue());
}
props.storeToXML(new FileOutputStream(System.getProperty("user.dir")
+ "\\ini\\" + "example.ini", false), "Here you can enter comment");
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

Output of the program

run:
access with the key java = >> James Gosling
access with the key linux = >> Linus Torvalds
access with the key microsoft = >> Bill Gates
— listing properties —
java=James Gosling
linux=Linus Torvalds
microsoft=Bill Gates
ORACLE Performance Tuning
PL/SQL programming best practices
How to program Java
BUILD SUCCESSFUL (total time: 0 seconds)

XML inside example.ini file

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here you can enter comment</comment>
<entry key="java">James Gosling</entry>
<entry key="book3">ORACLE Performance Tuning</entry>
<entry key="microsoft">Bill Gates</entry>
<entry key="linux">Linus Torvalds</entry>
<entry key="book2">PL/SQL programming best practices</entry>
<entry key="book1">How to program Java</entry>
</properties>

Source code of the program

Advertisements

3 thoughts on “How to read and write key/value pairs inside .ini file JAVA (XML store)

  1. Hi Tuna,
    Thanks, also can you suggest me how to handle ini file with sub headings like below

    [HEADING1][SUBHEADING]
    DBUSer=John
    DBPASSWD=Joe1234

    [HEADING2][SUBHEADING]
    DBUser=David
    DBPASSWD=Roger1234

    1. As far as I know, you must use prefixes while reading ini files values,
      properties file require a prefix like the following;

      dbone.username
      dbone.pass

      dbtwo.username
      dbtwo.pass

      Here I found a solution (helper class) may help you while reading group prefixed
      http://stackoverflow.com/questions/6696770/reading-java-property-groups-from-a-file\

      Or you can use ini library such as the following one for headers

      http://ini4j.sourceforge.net/index.html
      http://ini4j.sourceforge.net/tutorial/OneMinuteTutorial.java.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s