473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Replace text in text file

Hi

I would like to replace a string to an another into a text file (not a XML)
Have someone a example that I can take a look

ti
Samuel
Nov 15 '05 #1
2 48329

For the moment it works like this in my application

// Open a file for reading
StreamReader streamReader
streamReader = File.OpenText(fileName)
// Now, read the entire file into a strin
string contents = streamReader.ReadToEnd();
streamReader.Close()

// Write the modification into the same fil
StreamWriter streamWriter=File.CreateText(fileName);

streamWriter.Write(contents.Replace("String1", "String2"));
streamWriter.Close();
Nov 15 '05 #2
Samuel <an*******@discussions.microsoft.com> wrote:
For the moment it works like this in my application :

// Open a file for reading
StreamReader streamReader;
streamReader = File.OpenText(fileName);
// Now, read the entire file into a string
string contents = streamReader.ReadToEnd();
streamReader.Close();

// Write the modification into the same file
StreamWriter streamWriter=File.CreateText(fileName);

streamWriter.Write(contents.Replace("String1", "String2"));
streamWriter.Close();


And what don't you like about the way it works at the moment? It could
certainly be more memory efficient if the files are large and your
replacements can work on a line-by-line basis (read a line, do the
replacement, write the line, repeat) - but otherwise, that looks mostly
okay.

I'd suggest using the using (...) construct rather than calling Close
explicitly though, as otherwise your files won't get closed immediately
if you get an exception.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pankaj.khandelwal | last post by:
Hi, How can I bundle a text/gif file in the shared object. Basically I want to read the content of this text file but I cannot have it seperately on the disc. Any clues. ? Pankaj
1
by: David Dvali | last post by:
How can I load text from file to TextBox?
1
by: shapper | last post by:
Hello, I have a XLST file where I have the phrase "HERE". Something like: <xsl:text>HERE</xsl:text> I want to replace "HERE" by "UPDATED" and save the file.
4
by: moondaddy | last post by:
I need to edit the text in many files so I'm writing a small routine to do this. First I have a method that loops through all the files in a directory and passes the full file path to another...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
7
by: kushaldutt | last post by:
Hi all, I am having trouble finding and replacing text in a xml file. My issue is I have to find a text in a xml file and change it to some other text. But the text in some element tag for ex:...
4
by: alexvorn2 | last post by:
Hi! I use VB 2008 and I want to ask you how I can replace the text that is between "*" and "*" in a text file? If text file contains this: *dog* *cat* *bird* *lion* *something* .....end of...
2
by: King of the R.O.U.S.'s | last post by:
Hi All How do I replace selected text in a textarea with JavaScript? I have a text area that the user can select what they want then press a button that will pick up the selected text, make...
9
by: xtremebass | last post by:
hi every one. i am new to this forum. i have two doubts. one is , is it possible to read a text/html file in Jsp? if so, how can we find out the end of each line in the text/html file? ...
3
dlite922
by: dlite922 | last post by:
Hey guys, I need to maintain short list of messages in a text log file. I was just googling for the best way to delete the first or last line and insert a new one. Basically with each access...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.