Washington A. Ramos
Washington A. Ramos

Reputation: 924

Java - Writing, reading and modifying strings with parameters

So, I'm doing a project and now I have a question, so I would like your help :)

First, I already know how to write and read a .txt file, but I want something more than just x.hasNext().

I want to know how could write, read and modify a .txt file like .ini does. What? Simple (I think):

First, write a file like this:

[client1]
name=Bill Gates
nick=Uncle Bill
number=123456789
[client2]
name=Steve Jobs
nick=Stevie
number=987654321

And so many other parameters, just like above, but when I'm wanting to read a specific one (like name or nick of a certain "client") I can do it with easy (Ok, I know it will not be easy, but I think you understood :D)

So, if you already know what I want to learn, please, teach me :) If you don't, please explain me what you didn't understood :D

Thanks in advance for every helping

Upvotes: 2

Views: 314

Answers (2)

Matt
Matt

Reputation: 11805

The commons-configuration project from apache supports INI file formats:

HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(file);
Set<String> sectionNames = config.getSections();
SubnodeConfiguration section = config.configurationAt(sectionName);
String value = section.getString(keyName);

Check out the javadocs, it's pretty easy to use.

Upvotes: 0

paulsm4
paulsm4

Reputation: 121649

The format you describe is for a Windows .ini file, back from Windows 3.x days:

Perhaps the closest thing to a "standard format" in Java is a "properties" file; typically in the format "name=value":

If you were to write your own program and invent your own initialization file format, would not use an .ini file. Instead, I would recommend:

1) simple properties file (if possible)

 ... otherwise ...

2) an XML file (if you need multi-level, structured data)

However, if you want to read and write existing .ini files for an existing application, I would either:

1) write my own .ini parser (it isn't difficult)

 ... or ...

2) Download and run a library likke ini4j:

'Hope that helps!

Upvotes: 3

Related Questions