namespace com.hitrust.trustpay.client { using System; using System.Collections; using System.IO; using System.Text; public class IniFileParser { private string iFileName; private SortedList iKeyValue; public IniFileParser(string aFileName) { this.iFileName = aFileName; this.initial(); } public virtual string getTag(int aIndex) { return (string) this.iKeyValue.GetKey(aIndex); } public virtual string getValue(int aIndex) { return (string) this.iKeyValue.GetByIndex(aIndex); } public virtual string getValue(string aTag) { if (!this.iKeyValue.Contains(aTag)) { return null; } return (string) this.iKeyValue.GetByIndex(this.iKeyValue.IndexOfKey(aTag)); } private void initial() { try { string tLine; StreamReader tFile = new StreamReader(this.iFileName); StreamReader tBuff = new StreamReader(tFile.BaseStream, Encoding.UTF7); this.iKeyValue = new SortedList(); while ((tLine = tBuff.ReadLine()) != null) { if ((tLine.Length > 0) && (((tLine[0] != '#') && (tLine[0] != ';')) && (tLine[0] != ' '))) { this.parsingLine(tLine); } } tBuff.Close(); } catch (IOException) { throw new Exception("找不到配置文件!"); } } [STAThread] public static void Main(string[] arguments) { if (arguments.Length == 0) { Console.Out.WriteLine("Usage: java ReadSource [FileName]."); } else { IniFileParser tIni = new IniFileParser(arguments[0]); Console.Out.WriteLine("HiTRUST Java Utilities Class - IniFileParser Testing"); Console.Out.WriteLine("Initial File Name = [" + arguments[0] + "]"); Console.Out.WriteLine("Number of Elements = [" + tIni.numberOfElements() + "]"); for (int i = 0; i < tIni.numberOfElements(); i++) { Console.Out.WriteLine("[" + tIni.getTag(i) + "] - [" + tIni.getValue(i) + "]"); } } } public virtual int numberOfElements() { return this.iKeyValue.Keys.Count; } private void parsingLine(string aLine) { int tTagEnd = aLine.IndexOf("="); int tValueStart = tTagEnd + 1; int tValueEnd = aLine.Length; for (int i = tValueStart; i < tValueEnd; i++) { if (((aLine[i] == '#') || (aLine[i] == ';')) || (aLine[i] == ' ')) { tValueEnd = i; break; } if (aLine[i] == '"') { tValueEnd = aLine.LastIndexOf("\""); tValueStart++; break; } } this.iKeyValue.Add(aLine.Substring(0, tTagEnd), aLine.Substring(tValueStart, tValueEnd - tValueStart)); } public virtual void reload() { this.initial(); } } }