用StreamReader类的readline()方法当行数多的时候,出现数据丢失怎么办?

来源:百度知道 编辑:UC知道 时间:2024/07/05 14:57:33
我读一个文本文件,比如说读30行没问题,当变成31行的时候第一行的东西就丢了。
public static List<string> creatStringListByFile(string filepath)
{

FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
string content = "";
StringBuilder contents =new StringBuilder();
content = sr.ReadLine();
while (content != null)
{

contents .Append(content);
contents.Append(" ");
content = sr.ReadLine();

}

List<string> tokenstringlist = new List<string>();

tokenstringlist.AddRange(contents.ToString().Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
fs.Close();
sr.Close();

不会阿,你可以试着向控制台输出看看是什么情况,你可能是开始时把行写死了,把代码贴上来看看

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"c:\zhidao.txt",System.Text.Encoding.Default);
while ((line = file.ReadLine()) != null)
{
System.Console.WriteLine(line);
counter++;
}

file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.
System.Console.ReadLine();
我在控制台试了试,读取c盘下的一个zhidao.txt文件并统计出行数
按行读取文本文件,这样就行了,不太明白你写的那个泛型是什么意思

先占个楼.. 我试试 

同学 我是对的- -|

我的环境是.net2.0

代码复制你的

如下

        static void Main(string[] args)

  &n