Java properties with parameters
I just recently learned about the Properties class in java, and I'm trying to translate my old configuration file to be compatible with the Properties class. I want to know what would be the best way to setup a properties file for something like this:
Object type1
name obj1
age age1
Object type2
name obj2
age age2
I used spaces because I was using a Scanner to parse the file. I would create a List of Objec开发者_JAVA技巧ts that stored the different objects and each Object had certain parameters with them, which I would get from the config file. So as I read along the file I would get the properties and create the object and store into my List.
I don't want to use XML to create my properties file. I want something simple for a user to create and something that I could read easily in Java.
Any suggestions?
EDIT: I want to try to get something with no need of numbering. For example in XML you can do something like this:
<table>
<fields>
<field>
<type>type1</type>
</field>
<field>
<type>type2</type>
<field>
</fields>
</table>
Obviously it would be easier to make a XML file like this, but I want to implement in a non-XML way.
The property keys ought to be unique. I'd suggest to use a numerical infix.
field1.type = type1
field1.name = obj1
field1.age = age1
field2.type = type2
field2.name = obj2
field2.age = age2
Then you can gather them in an incremental loop as follows:
Properties properties = new Properties();
properties.load(input);
List<Field> fields = new ArrayList<Field>();
for (int i = 1; i < Integer.MAX_VALUE; i++) {
String type = properties.getProperty("field" + i + ".type");
if (type == null) break;
String name = properties.getProperty("field" + i + ".name");
String age = properties.getProperty("field" + i + ".age");
fields.add(new Field(type, name, age));
}
Unrelated to the concrete problem, I am confident that XML is way much easier to maintain and parse, for example with JAXB.
You should look at the java.util.preferences API.
The Preferences API is a hierarchical structure that will allow you to get the logical grouping of data that you are looking for.
It is much more powerful and configurable than the ages old file backed Properties
object.
There are lots of tutorials on how to use it.
You specific a PreferencesFactory
implementation and it can store the data in any way shape or format you desire, even in your previous format.
Import/Export: An Easy Way to Move Preferences Around
Let's face it: the Properties API is sooo simple, easy, and convenient to use. Once you've stored the preferences in a file, you can hand-edit them, zip, then ftp or email them so your poor, freezing colleague in the data center can get your application running with the correct settings and get out of there as soon as possible.
Here is how to use Preferences using a File as the backing store in a Properties file format.
So, is that it? No! Based on what you've learned from this article, it is not difficult to come up with your own cross-platform, back-end neutral Registry Editor that provides easy access to preferences trees on any platform -- now that surely beats hand-editing Properties files!
this is one possible way,
type1.name = obj1
type1.age = age1
type2.name = obj2
type2.age = age2
精彩评论