Java Code:
[Java]
Import java. io. BufferedReader;
Import java. io. FileReader;
Import java. io. IOException;
Import java. util. HashMap;
Import java. util. Properties;
Public class iniReader {
Protected HashMap <String, Properties> sections = new HashMap <String, Properties> ();
Private transient String currentSecion;
Private transient Properties current;
Public IniReader (String filename) throws IOException {
BufferedReader reader = new BufferedReader (new FileReader (filename ));
Read (reader );
Reader. close ();
}
Protected void read (BufferedReader reader) throws IOException {
String line;
While (line = reader. readLine ())! = Null)
{
ParseLine (line );
}
}
Protected void parseLine (String line ){
Line = line. trim ();
If (line. matches ("\ [. * \]")
{
CurrentSecion = line. replaceFirst ("\ [(. *) \]", "$1 ");
Current = new Properties ();
Sections. put (currentSecion, current );
} Else
If (line. matches (". * = .*"))
{
If (current! = Null)
{
Int I = line. indexOf ('= ');
String name = line. substring (0, I );
String value = line. substring (I + 1 );
Current. setProperty (name, value );
}
}
}
Public String getValue (String section, String name ){
Properties p = (Properties) sections. get (section );
If (p = null)
{
Return null;
}
String value = p. getProperty (name );
Return value;
}
}
Import java. io. BufferedReader;
Import java. io. FileReader;
Import java. io. IOException;
Import java. util. HashMap;
Import java. util. Properties;
Public class iniReader {
Protected HashMap <String, Properties> sections = new HashMap <String, Properties> ();
Private transient String currentSecion;
Private transient Properties current;
Public IniReader (String filename) throws IOException {
BufferedReader reader = new BufferedReader (new FileReader (filename ));
Read (reader );
Reader. close ();
}
Protected void read (BufferedReader reader) throws IOException {
String line;
While (line = reader. readLine ())! = Null)
{
ParseLine (line );
}
}
Protected void parseLine (String line ){
Line = line. trim ();
If (line. matches ("\ [. * \]")
{
CurrentSecion = line. replaceFirst ("\ [(. *) \]", "$1 ");
Current = new Properties ();
Sections. put (currentSecion, current );
} Else
If (line. matches (". * = .*"))
{
If (current! = Null)
{
Int I = line. indexOf ('= ');
String name = line. substring (0, I );
String value = line. substring (I + 1 );
Current. setProperty (name, value );
}
}
}
Public String getValue (String section, String name ){
Properties p = (Properties) sections. get (section );
If (p = null)
{
Return null;
}
String value = p. getProperty (name );
Return value;
}
}