Sample of Java reads INI files

xiaoxiao2021-03-06  43

/ *

* @ (#) Inireader.java

* CREATED ON 2004-10-14

* Created by James Fancy

* /

Import java.io.bufferedreader;

Import java.io.fileReader;

Import java.io.ioException;

Import java.util.hashmap;

Import java.util.properties;

/ **

* @Author James Fancy

* /

Public class inireader {

protected hashmap sections = new hashmap ();

PRIVATE TRANSIENT STRING CURRENTSECON;

PRIVATE TRANSIENT Properties CURRENT;

Public inireader (String filename) throws oException {

BufferedReader Reader = New BufferedReader (New FileReader);

Read (Reader);

Reader.Close ();

}

Protected void read (bufferedreader reader) throws oException {

String line;

WHILE ((line = reader.readline ())! = null) {

Parseline (LINE);

}

}

Protected Void Parseline (String line) {

Line = line.trim ();

IF (line.matches ("//[.*//]")) {

// If it is JDK 1.4 (excluding 1.4) below, modify

// if (line.startswith ("[") && line.endswith ("]")) {

IF (current! = null) {

Sections.put (currentsecion, current);

}

Currentsecion = line.replacefirst ("//[ (.*) //]", "$ 1");

// jdk is less than 1.4

// currentsection = line.substring (1, line.Length () - 1);

Current = new profment ();

} Else IF (line.matches (". * =. *"))) {

// jdk is less than 1.4

//} else if (Line.IndexOf ('=')> = 0) {

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) {

Properties P = (Properties) Sections.get (section); if (p == null) {

Return NULL;

}

String value = p.GetProperty (name);

Return Value;

}

}

Example:

Public static void main (string [] args) throws oException {

Inireader Reader = New Inireader ("E: //james//win.ini);

System.out.println (Reader.getValue ("MCI Extensions.bak", "ASF");

}

转载请注明原文地址:https://www.9cbs.com/read-59647.html

New Post(0)