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.Strea mReader sr = new System.IO.Strea mReader(@"C:\1. txt");
string all = sr.ReadToEnd();
sr.Close();
all = all.Replace("," ,".");
System.IO.Strea mWriter sw = new System.IO.Strea mWriter(@"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. 13 3976
<ml*******@gmai l.com> wrote: [replacing characters in a 500 MB text file] [...] string all = sr.ReadToEnd();
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.
Hi,
Below is a better solution:
string sourceFile = @"C:\1.txt";
string tempTarget = System.IO.Path. GetTempFileName ();
StreamReader reader = new System.IO.Strea mReader( sourceFile );
StreamWriter writer = new StreamWriter( tempTarget )
string line;
while ( (line = reader.ReadLine () )
writer.WriteLin e( 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*******@gmai l.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. . 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.Strea mReader sr = new System.IO.Strea mReader(@"C:\1. txt"); string all = sr.ReadToEnd(); sr.Close(); all = all.Replace("," ,"."); System.IO.Strea mWriter sw = new System.IO.Strea mWriter(@"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.
He probably wants to do a File.Move instead of a File.Copy for that last
one.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:Og******** ********@TK2MSF TNGP12.phx.gbl. .. Hi,
Below is a better solution:
string sourceFile = @"C:\1.txt"; string tempTarget = System.IO.Path. GetTempFileName ();
StreamReader reader = new System.IO.Strea mReader( sourceFile ); StreamWriter writer = new StreamWriter( tempTarget )
string line; while ( (line = reader.ReadLine () ) writer.WriteLin e( 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*******@gmai l.com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. .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.Strea mReader sr = new System.IO.Strea mReader(@"C:\1. txt"); string all = sr.ReadToEnd(); sr.Close(); all = all.Replace("," ,"."); System.IO.Strea mWriter sw = new System.IO.Strea mWriter(@"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.
Hi,
Yes, you are right :)
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:2I******** ************@gi ganews.com... He probably wants to do a File.Move instead of a File.Copy for that last one.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote in message news:Og******** ********@TK2MSF TNGP12.phx.gbl. .. Hi,
Below is a better solution:
string sourceFile = @"C:\1.txt"; string tempTarget = System.IO.Path. GetTempFileName ();
StreamReader reader = new System.IO.Strea mReader( sourceFile ); StreamWriter writer = new StreamWriter( tempTarget )
string line; while ( (line = reader.ReadLine () ) writer.WriteLin e( 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*******@gmai l.com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. .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.Strea mReader sr = new System.IO.Strea mReader(@"C:\1. txt"); string all = sr.ReadToEnd(); sr.Close(); all = all.Replace("," ,"."); System.IO.Strea mWriter sw = new System.IO.Strea mWriter(@"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.
Thanks guys, specially Ignacio.
I'll try next days but really seems to be a good solution. If I have
any more problems, I'll return to the post.
Thanks again.
Hi again, the "while" statement does not compile because the while
comparison must return true and we are doing an assigment statement.
ReadLine() returns string and must be bool. Besides, I cannot do
while (reader.ReadLin e() != String.Empty) because the file can contain
blank lines and this way we will go out of the while loop.
Any other idea?
Thanks.
It should be
while ( (line.reader.Re adLine()) != null)
Another suggestion, however - rather than having manual calls to Close,
you should use "using" statements round both the reader and the writer.
That way they automatically get closed whether an exception occurs or
not.
Jon
Hi guys,
The correct code, perfectly working, although I haven't tried with 500
MBytes .txt files:
-----------------------------------------------------------------------
string sourceFile = textBox1.Text;
string tempTarget = System.IO.Path. GetTempFileName ();
StreamReader reader = new System.IO.Strea mReader(sourceF ile);
StreamWriter writer = new StreamWriter(te mpTarget);
string line = String.Empty;
while ( (line = reader.ReadLine ()) != null)
{
writer.WriteLin e(line.Replace( txt1.Text,txt2. Text));
}
reader.Close();
writer.Close();
File.Delete(sou rceFile);
File.Move(tempT arget,sourceFil e);
----------------------------------------------------------------------------
I'm not able to do it with "using" clause :(
Thanks Jon.
<ml*******@gmai l.com> wrote: Hi guys,
The correct code, perfectly working, although I haven't tried with 500 MBytes .txt files:
----------------------------------------------------------------------- string sourceFile = textBox1.Text; string tempTarget = System.IO.Path. GetTempFileName (); StreamReader reader = new System.IO.Strea mReader(sourceF ile); StreamWriter writer = new StreamWriter(te mpTarget);
string line = String.Empty; while ( (line = reader.ReadLine ()) != null) { writer.WriteLin e(line.Replace( txt1.Text,txt2. Text)); } reader.Close(); writer.Close(); File.Delete(sou rceFile); File.Move(tempT arget,sourceFil e); ----------------------------------------------------------------------------
I'm not able to do it with "using" clause :(
You should be able to:
using (StreamReader reader = new StreamReader (sourceFile))
{
using (StreamWriter writer = new StreamWriter (tempTarget))
{
string line;
while ( (line=reader.Re adLine()) != null)
{
writer.WriteLin e (line.Replace(t xt1.Text, txt2.text);
}
}
}
File.Delete(sou rceFile);
File.Move(tempT arget,sourceFil e);
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jamait |
last post by:
Hi all,
I m trying to read in a text file into a datatable...
Not sure on how to split up the information though, regex or substrings...?
sample:
Col1 Col2 Col3
Col4
A0012430 REKAL TVÄTTMEDEL EKOMAX 0,5L ST 75.9000
|
by: Neil Patel |
last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n
Does anyone know how to seek to the end of the file and start reading backwards?
|
by: Sabin Finateanu |
last post by:
Hi I'm having problem reading a file from my program and I think it's from a
procedure I'm using but I don't see where I'm going wrong. Here is the code:
public bool AllowUsage()
{
OperatingSystem os = Environment.OSVersion;
AppDomain ad = Thread.GetDomain();
|
by: haibhoang |
last post by:
I have a Windows Service that is trying to parse a large (> 1Gig) text
file. I am keep getting OutOfMemoryException exception. Here is the
code that's having problem:
using (StreamReader streamReader = new
StreamReader(stream, Encoding.ASCII))
{
string line = "";
DateTime currentDate = DateTime.Now.Date;
while (streamReader.Peek() > -1)
|
by: Gustaf |
last post by:
I'm converting a program I made in Python once to C#, and while I'm at
it, I want to do some performance improvements. The program takes a set
of mailbox files (in readable formats, like Eudora and Thunderbird) and
extracts every message that is to/from a particular penpal (identified
by one or more email addresses). Messages in a mailbox file are
typically separated with lines such as these:
From ???@??? Fri Feb 06 00:26:08 2004
From -...
| |
by: leticia larrosa |
last post by:
Hi,
I try to read a file that have 8 bit character, but contain some
character
whose code is more than 128 (spanish character).
When I read this file using ASCII (Dim oRead As StreamReader = New
StreamReader("C:\...\name.txt", System.Text.Encoding.ASCII)), this
special characters aren't appear.
|
by: jeff M via .NET 247 |
last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I get the correct answer by taking the decimal valueand using that as an index to an array that will map to thecorrect EBCDIC value in hex.
By larger values, an example would be "AA" in EBCDIC hex wouldgive me the value of 63 in decimal (ASCII) when...
|
by: Drew Berkemeyer |
last post by:
Hello,
I'm using the following code to read a text file in VB.NET.
Dim sr As StreamReader = File.OpenText(strFilePath)
Dim input As String = sr.ReadLine()
While Not input Is Nothing
strReturn += input + vbCrLf
input = sr.Read
|
by: James Wong |
last post by:
Dear all,
I'm using StreamReader to read a text file containing BIG-5 data and found
that no matter which encoding method in StreamReader's construction
parameter, the BIG-5 contents become garbage under ReadLine method. Does
anybody have any idea on this issue?
Thanks for your attention and kindly help!
Regards,
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |