We often use such profiles in the program:
[section]
Key = Value
Below is an instance in this configuration file
[Server]
Address = 192.168.1.151
Port = 8000
Version = 1
INFO = 1
[User]
Name = kyle
PWD = 1234
[ID]
111; 222; 333
[End]
The source code for processing the configuration files that process this structure is provided:
Package org.kyle.util;
Import java.util. *;
Import java.io. *;
Public Class Cfgarea
{
Char m_start, m_end;
String m_commenttag = "#"; // In the # beginning with the configuration file, notes
String m_valuedelim = NULL;
String M_Areaname = NULL;
Hashmap m_content = new hashmap ();
String m_nextname = null;
Public cfgarea (Char AreatagStart, Char Areatagend)
{
THITAGSTART, AREATAGEND, NULL;
}
Public void reset ()
{
m_areaname = m_nextname;
m_nextname = null;
M_Content.clear ();
}
Public cfgarea (Char AreatagStart, Char Areatagend, String CTAG)
{
m_start = areatagstart;
m_end = areatagend;
IF (ctag! = null) m_commenttag = CTAG;
}
Public void setValuedelim (String VDLM)
{
m_valuedelim = vdlm;
}
Public string getName ()
{
Return M_Areaname;
}
Public String getValue (String Skey)
{
Return (String) M_Content.get (SKEY);
}
Public set getKeys ()
{
Return M_Content.KeySet ();
}
Public Boolean Readarea (BufferedReader Br)
{
Try
{
String aline = null;
While (aline = readcfgline (br))! = null)
{
IF (aline.trim (). Equals ("")) Continue;
IF (m_areaname == null)
{
m_areaname = getareaname (aline);
IF (m_areaname == null) Return False;
CONTINUE;
}
Else
{
IF (RESTORETAG (ALINE)) RETURN TRUE;
}
IF (M_Valuedelim! = NULL)
{
AddKeys (ALINE, M_VALUEDELIM);
}
Else
AddKeyPair (ALINE);
}
//System.out.println (m_content);
Return aline! = NULL;
}
Catch (Exception E) {
Debug.warning ("Readarea Exception In CFGGAREA);
}
Return False;
}
Private string readcfgline (bufferedreader thereader)
Throws oException
{
String aline = null;
Do {
Aline = thereader.readline ();
While (aline! = null && (aline.trim (). Equals ("") || aline.trim (). StartSwith (m_commenttag));
Return aline == NULL? NULL: aline.trim ();
}
Private boolean isareatag (string aline)
{
Char [] linechars = aline.tocharray ();
Return Linechars [0] == m_start && linechars [linechars.length - 1] == m_end;
}
Private string getareaname (String aline)
{
IF (! isaretag (aline)) Return NULL;
StringBuffer SB = New StringBuffer;
sb.deletecharat (0);
sb.deletecharat (sb.Length () - 1);
Return sb.toString ();
}
Private void addkeypair (String aline)
{
StringTokenizer ST = New StringTokenizer (aline, "=");
String skey = null, sval = null;
IF (st.hasmoretokens ())
SKEY = st.nextToken ();
IF (st.hasmoretokens ())
sval = st.nextToken ();
IF (SKEY == null) return;
M_Content.Put (SKEY, SVAL);
}
Private Void AddKeys (String Aline, String Delim)
{
StringTokenizer ST = New StringTokenizer (ALINE, DELIM);
String Skey = NULL;
While (st.hasmoretokens ())
{
SKEY = st.nextToken ();
m_content.put (SKEY, M_Areaname);
}
}
Private Boolean Restoretag (String AreatAg)
Throws oException
{
IF (! isaretag (allatag)) Return False;
m_nextname = getareaname (area);
Return True;
}
}
Let's take a look at this class:
Private static hashtable property = new hashtable ();
Try {
BufferedReader PBR = New BufferedReader (New INPUTSTREAM (NewFileInputStream ("Property.cfg"))))
Cfgarea ca = new cfgarea ('[', ']'); // In addition to using [] to identify section, you can also use other characters.
Ca.readarea (PBR);
Hashtable h1 = new hashtable ();
H1.PUT ("ServerName", ca.getname ());
H1.PUT ("Address", Ca.getValue ("Address"));
H1.PUT ("Port", Ca.getValue ("port"));
Properties.Put (ca.getname (), h1);
Ca.Reset ();
// Start reading a section
Ca.readarea (PBR);
Hashtable h2 = new hashtable ();
H2.PUT ("User", ca.getname ());
H2.PUT ("Name", Ca.getValue ("Name"));
H2.PUT ("PWD", CA.GetValue ("PWD"));
Properties.put (ca.getname (), h2);
Ca.Reset ();
// The third section is different from the top two, and he can regard him as only KEY.
Ca.setValuedelim (";"); // k 分;;
Ca.readarea (PBR);
Hashtable h3 = new hashtable ();
Iterator Ikey = ca.getKeys (). Iterator ();
While (iKey.hasNext ()) {
H3.PUT (String) iKey.next (), ca.getname ());
}
Properties.Put (ca.getname (), h3);
Ca.Reset ();
PBR.CLOSE ();
}
Catch (Exception E) {
Properties.clear ();
E.PrintStackTrace ();
}