日期:2014-05-18 浏览次数:20851 次
public class TCount { private string _Url; public string Url { get { return _Url; } set { _Url = value; } } private int _Count; public int Count { get { return _Count; } set { _Count = value; } } public TCount(string _Url, int _Count) { this._Url = _Url; this._Count = _Count; } } public class DoCount { private string _FilePath; public string FilePath { get { return _FilePath; } set { _FilePath = value; } } public DoCount(string _FilePath) { this._FilePath = _FilePath; } /// <summary> /// 读取数据,并统计 /// </summary> /// <returns></returns> public IList<TCount> CountUrl() { IList<TCount> IT = new List<TCount>(); StreamReader sr = new StreamReader(this._FilePath); while (sr.ReadLine()!=null) { IT = ReCorrectCount(IT, sr.ReadLine()); } sr.Close(); return IT; } private IList<TCount> ReCorrectCount(IList<TCount> IT, string url) { int i = 0; bool blnIn = false; //用于标识url是否已存在于IT中 foreach(TCount T in IT){ if (T.Url == url) { //如果存在,则更新统计数 IT[i].Count += 1; blnIn = true; break; } i++; } if (!blnIn)//如果不存在则添加新项 IT.Add(new TCount(url, 1)); return IT; } }