现在要读入一个有上千万条数据的文本文件,我用
openFileDialog1.Filter="文本文件(*.txt)|*.txt"; if (openFileDialog1.ShowDialog()== DialogResult.OK)
{
StreamReader sreader = new StreamReader(new FileStream(openFileDialog1.FileName, FileMode.Open), System.Text.Encoding.Default);
//循环读取文本文件中的数据
while (sreader.Peek()>0)
{
string str = sreader.ReadLine();
string[] strInfo = str.Split(new char[] { ' ' }); string strID = strInfo[0].ToString();
string strCode = strInfo[1].ToString();
}
如果是一行一行读是否会影响到读取的效率,怎样提升读取的效率
openFileDialog1.Filter="文本文件(*.txt)|*.txt"; if (openFileDialog1.ShowDialog()== DialogResult.OK)
{
StreamReader sreader = new StreamReader(new FileStream(openFileDialog1.FileName, FileMode.Open), System.Text.Encoding.Default);
//循环读取文本文件中的数据
while (sreader.Peek()>0)
{
string str = sreader.ReadLine();
string[] strInfo = str.Split(new char[] { ' ' }); string strID = strInfo[0].ToString();
string strCode = strInfo[1].ToString();
}
如果是一行一行读是否会影响到读取的效率,怎样提升读取的效率
解决方案 »
- asp.net ajax控件,为何在我机器上运行良好,上传服务器后,我在浏览器端运行,报缺少对象错误!如何解决?
- C# 数据库显示问题
- winfrm中的datagrid问题
- 网络软件中更新远程数据库记录是使用WEB服务还是Socket或其他?
- 我的这个为什么会报错?
- 如何在对话框中动态生成一个textBox?
- 怎样通过应用程序访问网络(局域网,如:\\10.1.12.3\MS\)上文件,可能需要用户名和密码!
- 超难问题把一张表插入到远端数据库中问题
- 如何将数据库表的一列的数据读取到一个属性里呢
- C#中,我怎么找不到目录对话框,C#中应该有提供吧
- 请教个问题!在类开始写[assembly:....的什么作用啊?
- 如何将数据库连接的Data Source 改成相对路径连接
string all=reader.ReadToEnd();
可以一次全部读取到内存。
StreamReader sreader = new StreamReader(new FileStream(openFileDialog1.FileName, FileMode.Open)。。
为什么不是
StreamReader sreader = new StreamReader(openFileDialog1.FileName。。string[] strInfo = str.Split(new char[] { ' ' });
为什么不是:
string[] strInfo = str.Split(' ' ); //这是一个params
更简单的是:
string[] strInfo = str.Split();//默认WhiteSpace既然是格式化的文本,为什么不用TextFieldPaser呢?没事练字玩?