使用StreamReader读取重的.TXT文件 [英] Reading heavy .TXT file with StreamReader

查看:69
本文介绍了使用StreamReader读取重的.TXT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须读一个非常重的文本普通文件(通常是.txt

扩展名))并用另一个给定字符替换给定字符

in all inside文件。我的应用程序工作得非常好

下面显示的代码(代码放在一个buttonclick事件后,

在普通的OpenFileDialog中选择文件):


---------------------------------

System.IO .StreamReader sr = new System.IO.StreamReader(@" C:\\\ ttxt");

string all = sr.ReadToEnd();

sr .Close();

all = all.Replace(",","。");

System.IO.StreamWriter sw = new System。 IO.StreamWriter(@C:\1.txt",

false);

sw.Write(all);

sw.Flush();

sw.Close();

MessageBox.Show(" Done!");

- -------------------------------


问题是用户现在需要阅读超过500MB的.txt文件。

难以置信和地狱般但是真实!


用户现在运行应用程序并在选择.txt文件后用

OpenFileDialog,应用程序开始处理但是inmediatel crashes

或挂起,我不记得了。


我需要一个很好的性能方式来阅读如此繁重的.txt文件。阅读

文件在不同的步骤中会做到吗?我的意思是,使用MemoryStream对象读取

字节或类似的数组..........


任何帮助将不胜感激。

I must read a very heavy-weight text plain file (usually .txt
extension) )and replace a given character with another given character
in all text inside the file. My application was working pretty well
with this below shown code (code placed in a buttonclick event after
selecting the file in a normal OpenFileDialog):

---------------------------------
System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\1.txt");
string all = sr.ReadToEnd();
sr.Close();
all = all.Replace(",",".");
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\1.txt",
false);
sw.Write(all);
sw.Flush();
sw.Close();
MessageBox.Show("Done!");
---------------------------------

The problem is that user now needs to read over 500MBytes .txt files.
Incredible and hellish but true!

User now runs the application and after selecting .txt file with
OpenFileDialog, application starts processing but inmediatelly crashes
or hangs, I don''t remember.

I need a good performance way for reading so heavy .txt files. Reading
the file in different steps would do it? I mean, reading in arrays of
bytes or similar, with MemoryStream object..........

Any help will be greatly appreciated.

推荐答案

< ml ******* @ gmail.com>写道:
<ml*******@gmail.com> wrote:
[替换500 MB文本文件中的字符]
[...]
string all = sr.ReadToEnd();
[replacing characters in a 500 MB text file]
[...]
string all = sr.ReadToEnd();




这意味着你一次将整个文件读入内存。

相反,尝试将StreamReader和StreamWriter打开/>
时间。从一个文件中读取一行并将其写入另一个文件,然后

完成后删除第一个文件并将其替换为第二个文件。

您将只使用记忆占用了一行,而不是

从不知名的地方掏出500 MB。


P.



This means you''re reading the entire file into memory at once.
Instead, try having a StreamReader and a StreamWriter open at the same
time. Read a line from one file and write it to the other file, then
delete the first file when done and replace it with the second one.
You will only use the memory taken up by one line, rather than
slurping up 500 MB out of nowhere.

P.



下面是一个更好的解决方案:


string sourceFile = @" C:\1.txt" ;;

string tempTarget = System.IO.Path.GetTempFileName();


StreamReader reader = new System.IO.StreamReader(sourceFile);

StreamWriter writer = new StreamWriter(tempTarget)

string line;

while((line = reader.ReadLine())

writer.WriteLine(line.Replace(",","。"))

reader.Close();

writer.Close();

File.Delete(来源);

File.Copy(目标,来源);

欢呼,


-

Ignacio Machin,

igna cio.machin AT dot.state.fl.us

佛罗里达州交通局

< ml ******* @ gmail.com>在消息中写道

news:11 ********************** @ f14g2000cwb.googlegr oups.com ...
Hi,
Below is a better solution:

string sourceFile = @"C:\1.txt";
string tempTarget = System.IO.Path.GetTempFileName();

StreamReader reader = new System.IO.StreamReader( sourceFile );
StreamWriter writer = new StreamWriter( tempTarget )

string line;
while ( (line = reader.ReadLine() )
writer.WriteLine( line.Replace( ",",".") )
reader.Close();
writer.Close();
File.Delete( source);
File.Copy( target, source);
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ml*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
我必须阅读一个非常重的文本普通文件(通常是.txt
扩展名))并在文件内的所有文本中用另一个给定字符替换给定字符。我的应用程序工作得非常好
下面显示的代码(在正常的OpenFileDialog中选择文件后,代码放在一个buttonclick事件中):

------ ---------------------------
System.IO.StreamReader sr = new System.IO.StreamReader(@" C: \ .txt");
string all = sr.ReadToEnd();
sr.Close();
all = all.Replace(",","。 ");
System.IO.StreamWriter sw = new System.IO.StreamWriter(@" C:\\ ttxt",
false);
sw.Write(全部);
sw.Flush();
sw.Close();
MessageBox.Show(" Done!");
-------- -------------------------

问题是用户现在需要读取超过500MB的.txt文件。 />令人难以置信和地狱般但真实!

用户现在运行应用程序并在选择带有
OpenFileDialog的.txt文件后,应用程序开始处理但是在中途崩溃
或挂起,我不知道不记得了。

我需要一个好的表演阅读如此沉重的.txt文件的方法。以不同的步骤阅读文件会做到吗?我的意思是,使用MemoryStream对象读取
字节或类似的数组..........

任何帮助将不胜感激。
I must read a very heavy-weight text plain file (usually .txt
extension) )and replace a given character with another given character
in all text inside the file. My application was working pretty well
with this below shown code (code placed in a buttonclick event after
selecting the file in a normal OpenFileDialog):

---------------------------------
System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\1.txt");
string all = sr.ReadToEnd();
sr.Close();
all = all.Replace(",",".");
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\1.txt",
false);
sw.Write(all);
sw.Flush();
sw.Close();
MessageBox.Show("Done!");
---------------------------------

The problem is that user now needs to read over 500MBytes .txt files.
Incredible and hellish but true!

User now runs the application and after selecting .txt file with
OpenFileDialog, application starts processing but inmediatelly crashes
or hangs, I don''t remember.

I need a good performance way for reading so heavy .txt files. Reading
the file in different steps would do it? I mean, reading in arrays of
bytes or similar, with MemoryStream object..........

Any help will be greatly appreciated.


他可能想要一个File.Move而不是File.Copy用于最后一个

一个。


Ignacio Machin(.NET / C#MVP)" < ignacio.machin AT dot.state.fl.us>在消息新闻中写了

:Og **************** @ TK2MSFTNGP12.phx.gbl ...
He probably wants to do a File.Move instead of a File.Copy for that last
one.

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Og****************@TK2MSFTNGP12.phx.gbl...


下面是一个更好的解决方案:

字符串sourceFile = @" C:\1.txt" ;;
字符串tempTarget = System.IO.Path.GetTempFileName ();

StreamReader reader = new System.IO.StreamReader(sourceFile);
StreamWriter writer = new StreamWriter(tempTarget)

string line;
while((line = reader.ReadLine())
writer.WriteLine(line.Replace(",","。"))
reader.Close();
writer.Close();
File.Delete(source);
File.Copy(target,source);

欢呼,

- -
Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局

< ml ******* @ gmail.com>写在消息中
新闻:11 ********************** @ f14g2000cwb.googlegr oups.com ...
Hi,
Below is a better solution:

string sourceFile = @"C:\1.txt";
string tempTarget = System.IO.Path.GetTempFileName();

StreamReader reader = new System.IO.StreamReader( sourceFile );
StreamWriter writer = new StreamWriter( tempTarget )

string line;
while ( (line = reader.ReadLine() )
writer.WriteLine( line.Replace( ",",".") )
reader.Close();
writer.Close();
File.Delete( source);
File.Copy( target, source);
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ml*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
我必须读一个非常重的文本普通文件(你sually .txt
扩展))并在文件内的所有文本中用另一个给定字符替换给定字符。我的应用程序工作得非常好
下面显示的代码(在正常的OpenFileDialog中选择文件后,代码放在一个buttonclick事件中):

------ ---------------------------
System.IO.StreamReader sr = new System.IO.StreamReader(@" C: \ .txt");
string all = sr.ReadToEnd();
sr.Close();
all = all.Replace(",","。 ");
System.IO.StreamWriter sw = new System.IO.StreamWriter(@" C:\\ ttxt",
false);
sw.Write(全部);
sw.Flush();
sw.Close();
MessageBox.Show(" Done!");
-------- -------------------------

问题是用户现在需要读取超过500MB的.txt文件。 />令人难以置信和地狱般但真实!

用户现在运行应用程序并在选择带有
OpenFileDialog的.txt文件后,应用程序开始处理但是在中途崩溃
或挂起,我不知道不记得了。

我需要一个好的表演阅读如此沉重的.txt文件的方法。以不同的步骤阅读文件会做到吗?我的意思是,使用MemoryStream对象读取
字节或类似的数组..........

任何帮助将不胜感激。
I must read a very heavy-weight text plain file (usually .txt
extension) )and replace a given character with another given character
in all text inside the file. My application was working pretty well
with this below shown code (code placed in a buttonclick event after
selecting the file in a normal OpenFileDialog):

---------------------------------
System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\1.txt");
string all = sr.ReadToEnd();
sr.Close();
all = all.Replace(",",".");
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\1.txt",
false);
sw.Write(all);
sw.Flush();
sw.Close();
MessageBox.Show("Done!");
---------------------------------

The problem is that user now needs to read over 500MBytes .txt files.
Incredible and hellish but true!

User now runs the application and after selecting .txt file with
OpenFileDialog, application starts processing but inmediatelly crashes
or hangs, I don''t remember.

I need a good performance way for reading so heavy .txt files. Reading
the file in different steps would do it? I mean, reading in arrays of
bytes or similar, with MemoryStream object..........

Any help will be greatly appreciated.



这篇关于使用StreamReader读取重的.TXT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆