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:
And by practical I mean:
List<Header> headers = read_file(headerFileName); for(Header header : headers){ MyTable table = new MyTable(header.contentType); table.loadFromFile(header.filename); }
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.
I recommend using XML files and using JAXB to load them.
Why? Because of the following pros:
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
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>
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.
Comments