Practical parameter file reading in java

Juh_

I am starting with java and I am wondering which (text) file format I should use to read some parameter sets, such as:

Item1:   // the item name is not important
  - filename: item1.txt
  - contentType: individual
  - ...

Item2:
  - filename: item2.txt
  - contentType: group
  - ...

...

The purpose is to give a list of files to be loaded into a DB, as well as some description of file content.

So my question is:

  • What practical parameter file format should I use?

And by practical I mean:

  1. no (additional) external libraries required, so typically "standard" java and spring (the framework used)
  2. low development cost: easy parsing of the loaded file content, such as:

List<Header> headers = read_file(headerFileName); for(Header header : headers){ MyTable table = new MyTable(header.contentType); table.loadFromFile(header.filename); }

  1. file format readability (yaml'd be nice, but it seems to require an external lib)

Note: this question is similar to What is the best practice for reading property files in Java EE?, but I don't know much about the java ecosystem so I cannot be sure (eg. I understood that spring is an alternative to JavaEE). Here I tried to be more precise on my needs, and in particular on the "shape" of the parameters.

icza

I recommend using XML files and using JAXB to load them.

Why? Because of the following pros:

  • Because it is awefully simple.
  • No external libraries are needed.
  • The config file is well readable (simple XML), it can be edited with any text editors or advanced XML editors.
  • It is flexible enough to add other data later on to the parameters.
  • Also very easy to modify/save parameters from code at runtime (see at the end).
  • Thanks to XML you don't have to worry about character encoding (like in case of properties files).

Modelling:

First you need to create classes to "model" your parameters:

class Parameters {
    @XmlElement(name = "item")
    public List<Item> items;
}

class Item {
    public String fileName;
    public String contentType;
}

Example input XML file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parameters>
    <item>
        <fileName>item1.txt</fileName>
        <contentType>individual</contentType>
    </item>
    <item>
        <fileName>item2.txt</fileName>
        <contentType>group</contentType>
    </item>
</parameters>

Loading the parameters

And this is how you can load it, it's only 1 method call:

Parameters p = JAXB.unmarshal(new File("params.xml"), Parameters.class);

for (Item item : p.items)
    System.out.println(item.fileName + ": " + item.contentType);

Output:

item1.txt: individual
item2.txt: group

Alternative (simplified) XML input

To make the input XML file shorter, more easily readable, we can make the following change:

class Item {
    @XmlAttribute
    public String fileName;
    @XmlAttribute
    public String contentType;
}

Here we basically specified to store/read the data of an Item as XML attributes and not as child elements. With this modification the input XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parameters>
    <item fileName="item1.txt" contentType="individual" />
    <item fileName="item2.txt" contentType="group" />
</parameters>

Modifying and Saving parameters at runtime

If we want to modify the parameters and save them at runtime, it is just as easy as loading them: one line only. Below I modify the first item, and I also create and add a new third item:

// Modify item #1
p.items.get(0).fileName = "item11.txt";
p.items.get(0).contentType = "short";

// Create and add a new item
Item item3 = new Item();
item3.fileName = "item3.txt";
item3.contentType = "newtype";
p.items.add(item3);

// Save the modified parameters: 1 line:
JAXB.marshal(p, new File("params-out.xml"));

Output of the modified parameters:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parameters>
    <item fileName="item11.txt" contentType="short"/>
    <item fileName="item2.txt" contentType="group"/>
    <item fileName="item3.txt" contentType="newtype"/>
</parameters>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related