【发布时间】:2013-11-17 10:54:52
【问题描述】:
我只是想读取一个大的 CSV 文件并将 Stream 位置保存在一个列表中。之后,我必须从列表中读取位置并将 Streamreader 的位置设置为该字符并读取一行! 但是在我阅读第一行并返回流位置之后
StreamReader r = new StreamReader("test.csv");
r.readLine();
Console.WriteLine(r.BaseStream.Position);
我得到“177”,这是文件中的总字符数! (这只是一个简短的示例文件) 我没有在这里找到任何对我有帮助的东西!
为什么?
完整方法:
private void readfile(object filename2)
{
string filename = (string)filename2;
StreamReader r = new StreamReader(filename);
string _top = r.ReadLine();
top = new Eintrag(_top.Split(';')[0], _top.Split(';')[1], _top.Split(';')[2]);
int siteindex = 0, index = 0;
string line;
sitepos.Add(r.BaseStream.Position); //sitepos is the a List<int>
while(true)
{
line = r.ReadLine();
index++;
if(!string.IsNullOrEmpty(line))
{
if (index > seitenlaenge)
{
siteindex++;
index = 1;
sitepos.Add(r.BaseStream.Position);
Console.WriteLine(line);
Console.WriteLine(r.BaseStream.Position.ToString());
}
}
else break;
maxsites = siteindex;
}
reading = false;
}
文件如下所示:
name;age;city
Simon;20;Stuttgart
Daniel;34;Ostfildern
等等 这是一个程序练习: http://clean-code-advisors.com/ressourcen/application-katas (Katas CSV 查看器)我目前在识字 3
【问题讨论】:
-
您的 CSV 文件有多少行?因为如果它只有 1 行,在 StreamReader 上调用
ReadLine()应该会让您感到惊讶,因为您会走到最后。 -
测试文件有 11 行,最终文件大约 1,5GB 大 ;)
-
好的,你能出示你的
testfile以及你用来阅读它的确切代码吗? -
对我有用的解决方案在这里:stackoverflow.com/a/22975649/718033
标签: c# stream position streamreader