-3

I have some entry parametres in my Java SE application. I would like to ask you what is the best format for saving these information outside application itself for easy change/loading/saving parametres?

For your information, there are 6 numbers I have to load into my application. One of the file is default configuration. To the others I would like to save configuration created by users.

My first idea has been .xml file. If it is a god one, can you tell me what parser is the best one? I mean I've found many ways how to read from .xml file so which one from the known ones is the one I should choose?

Martin Plávek
  • 335
  • 1
  • 3
  • 17

2 Answers2

2

I prefer property files for simple properties.

Example reading from properties:

package com.mkyong.properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class App {
  public static void main(String[] args) {

    Properties prop = new Properties();
    InputStream input = null;

    try {

        input = new FileInputStream("config.properties");

        // load a properties file
        prop.load(input);

        // get the property value and print it out
        System.out.println(prop.getProperty("database"));
        System.out.println(prop.getProperty("dbuser"));
        System.out.println(prop.getProperty("dbpassword"));

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  }
}

And in the config.properties:

dbpassword=password
database=localhost
dbuser=mkyong

[EDIT] Per request, writing to properties:

package com.mkyong.properties;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class App {
  public static void main(String[] args) {

    Properties prop = new Properties();
    OutputStream output = null;

    try {

        output = new FileOutputStream("config.properties");

        // set the properties value
        prop.setProperty("database", "localhost");
        prop.setProperty("dbuser", "mkyong");
        prop.setProperty("dbpassword", "password");

        // save properties to project root folder
        prop.store(output, null);

    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
  }
}
Yuri
  • 2,008
  • 17
  • 36
1

Plain .properties or .xml are equivalently good solutions

Reading from .properties is trivial

Properties prop = new Properties();
InputStream input = null;

input = new FileInputStream("config.properties");
prop.load(input);

System.out.println(prop.getProperty("propertyname"));

As for .xml you can consider dom/sax or adding spring to your project.

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63