How to parse an INI file?

I use ini4j to parse an INI file. It doesn't work if the ini file is defined as below:[Main]x : 1y : 2It failed to parse it because it use colon instead of "=". How can I let the ini4j recognize colon?Thanks
[250 byte] By [youhaodiyia] at [2007-11-27 9:50:35]
# 1

> I use ini4j to parse an INI file. It doesn't work if

> the ini file is defined as below:

>

> [Main]

> x : 1

> y : 2

>

> It failed to parse it because it use colon instead of

> "=". How can I let the ini4j recognize colon?

>

> Thanks

never used ini4j personally, but you could use something like this:

import java.io.*;

import java.util.HashMap;

public class IniReader {

private static BufferedReader fileReader;

public static HashMap<String, String> parseFile(String filename, String separatorToken)

throws FileNotFoundException, IOException {

fileReader = new BufferedReader(new FileReader(filename));

String input;

HashMap<String, String> properties = new HashMap<String, String>();

while( (input = fileReader.readLine()) != null ) {

String[] pair = input.split(separatorToken);

if( pair.length == 2 ) {

properties.put(pair[0].trim(), pair[1].trim());

}

}

fileReader.close();

return properties;

}

}

Navy_Codera at 2007-7-13 0:19:25 > top of Java-index,Java Essentials,Java Programming...
# 2
ini4j support other features which your code doesn't. For example, an ini file may include different groups:[Main]x : 1y : 2[Sub]...[Sub1]...
youhaodiyia at 2007-7-13 0:19:25 > top of Java-index,Java Essentials,Java Programming...
# 3

> ini4j support other features which your code doesn't.

> For example, an ini file may include different

> groups:

>

> [Main]

> x : 1

> y : 2

>

> [Sub]

> ...

>

> [Sub1]

> ...

Well then - now you've given me something to play with for a little while... ;-) (Sorry I couldn't help you out, there. I'm sure someone else here has used the tool and will be able to give you a definitive answer on this.)

Navy_Codera at 2007-7-13 0:19:25 > top of Java-index,Java Essentials,Java Programming...
# 4

Well then convert your file to use "=", since that is the standard format for an ini file.

If you want to use a custom format, then you need to write custom code.

Why do you think there are defined standards for stuff like this? If everybody created their own standards programming would be a mess.

camickra at 2007-7-13 0:19:25 > top of Java-index,Java Essentials,Java Programming...
# 5

> Well then convert your file to use "=", since that is

> the standard format for an ini file.

>

Best suggestion yet.

> If you want to use a custom format, then you need to

> write custom code.

>

See below for a fair starting point.

> Why do you think there are defined standards for

> stuff like this? If everybody created their own

> standards programming would be a mess.

Makes a good point, doesn' he?

import java.io.*;

import java.util.HashMap;

class Group {

private HashMap<String, String> props;

public Group() {

props = new HashMap<String, String>();

}

public void addPair(String key, String value) {

props.put(key, value);

}

public String getValue(String key) {

return props.get(key);

}

}

public class IniReader {

private static BufferedReader fileReader;

public static HashMap<String, Group> parseFile(String filename, String separatorToken)

throws FileNotFoundException, IOException {

fileReader = new BufferedReader(new FileReader(filename));

String input;

HashMap<String, Group> groups = new HashMap<String, Group>();

Group currGroup = new Group();

while( (input = fileReader.readLine()) != null ) {

if( input.trim().length() > 0 &&

input.trim().charAt(0) == '[' &&

input.trim().charAt(input.trim().length() - 1) == ']' ) { // replace with regex

currGroup = new Group();

groups.put(input.trim(), currGroup);

}

String[] pair = input.split(separatorToken);

if( pair.length == 2 ) {

currGroup.addPair(pair[0].trim(), pair[1].trim());

}

}

fileReader.close();

return groups;

}

}

Like I said - nothing spectacular, but its at least a beginning. (I like tinkering with stuff like this...)

Navy_Codera at 2007-7-13 0:19:25 > top of Java-index,Java Essentials,Java Programming...
# 6
Ok, thanks.
youhaodiyia at 2007-7-13 0:19:25 > top of Java-index,Java Essentials,Java Programming...