怎么样控制读取文件(txt)特定的行

给你贴个C#@例子
using System;
using System.IO;
using System.Collections;

namespace brightheroes
{
///

1<summary>   
2/// 本类主要是为了以行的形式来处理文本文件   
3/// </summary>

public class TxtHelper
{
private int _LineNumber;
private string _FilePath;

///

1<summary>   
2/// 文件总行数   
3/// </summary>

public int LineNumber
{
get{return this._LineNumber;}
}

///

1<summary>   
2/// 文件路径   
3/// </summary>

public string FilePath
{
get{return this._FilePath;}
}

private ArrayList fileLine;

///

1<summary>   
2/// 读取文件到ArrayList里面去,按照行号排列   
3/// </summary>

///

1<param name="FilePath"/>

public TxtHelper(string FilePath)
{
this._FilePath = FilePath;
StreamReader sr = new StreamReader(FilePath);
fileLine = new ArrayList();
int i = 0;
while(sr.Peek() > -1)
{
fileLine.Insert(i,sr.ReadLine());
i = i + 1;
}

this._LineNumber = i;
sr.Close();
}

///

1<summary>   
2/// 返回某一行的内容   
3/// </summary>

///

1<param name="LineIndex"/>

///

1<returns></returns>

public string ReadLine(int LineIndex)
{
return this.fileLine[LineIndex].ToString();
}

///

1<summary>   
2/// 替换某一行的内容,如果不存在该行,则加入到末尾。   
3/// </summary>

///

1<param name="LineIndex"/>

///

1<param name="LineValue"/>

///

1<returns></returns>

public void WriteLine(int LineIndex,string LineValue)
{
if(LineIndex <= this._LineNumber)
{
this.fileLine[LineIndex] = LineValue;
}
else
{
this.fileLine.Insert(this._LineNumber,LineValue);
this._LineNumber += 1;
}
}

///

1<summary>   
2/// 插入某行到某处   
3/// </summary>

///

1<param name="LineIndex"/>

///

1<param name="LineValue"/>

public void InsertLine(int LineIndex,string LineValue)
{
if(LineIndex <= this._LineNumber)
{
this.fileLine.Insert(LineIndex,LineValue);
}
else
{
this.fileLine.Insert(this._LineNumber,LineValue);
this._LineNumber += 1;
}
}

///

1<summary>   
2/// 覆盖原文件   
3/// </summary>

public void Save()
{
StreamWriter sw = new StreamWriter(this._FilePath);
for(int i = 0;i

 1<this.fileline.count;i++) <summary="" sw.close();="" sw.writeline(this.fileline[i]);="" {="" }="">   
 2/// 生成新的文件   
 3///    
 4/// <param name="FilePath"/>   
 5public void Save(string FilePath)   
 6{   
 7StreamWriter sw = new StreamWriter(FilePath);   
 8for(int i = 0;i&lt;this.fileLine.Count;i++)   
 9{   
10sw.WriteLine(this.fileLine[i]);   
11}   
12sw.Close();   
13}   
14}   
15}</this.fileline.count;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus