2

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

3
  • 3
    Some file formats have specialized libraries for parsing and editing them. You can also write your own to handle it in a manner that you want. For INI files, there's ini4j. Under the hood they'll still be using the same file reading/access functionality like using a buffered reader, but they present a nicer interface to the files. Commented May 6, 2012 at 19:52
  • IMHO if there is a library for parsing a kind of file, then use it, don't write your own Commented May 6, 2012 at 19:56
  • @Luiggi Mendoza - in this case, it could arguably be both easier and more effective just to dash off your own, simple .ini parser class. IMHO... Commented May 6, 2012 at 19:57

2 Answers 2

3

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.