473,408 Members | 1,741 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,408 software developers and data experts.

Why is StreamWriter not working?

Im trying to write a class to replace strings in a text file but for some reason StreamWriter will not work in my class. Its not throwing any exceptions or anything its just not writing to the text file. Also I used to have a destructor in my class but it was throwing an exception when I tried to close or dispose outFile.

Expand|Select|Wrap|Line Numbers
  1. class TextReplacer
  2. {
  3.     private StreamReader inFile;
  4.     private StreamWriter outFile;
  5.  
  6.     public TextReplacer(StreamReader _in, StreamWriter _out)
  7.     {
  8.         inFile = _in;
  9.         outFile = _out;
  10.  
  11.         outFile.WriteLine("TESTING!!!"); // DOES NOT WORK
  12.     }
  13.  
  14.     public void Repace(string find, string replace)
  15.     {
  16.         string line = inFile.ReadLine();
  17.  
  18.         if (line != null)
  19.         {
  20.             line = line.Replace(find, replace);
  21.  
  22.             Console.WriteLine(line);
  23.  
  24.             outFile.WriteLine(line); // DOES NOT WORK
  25.  
  26.             Repace(find, replace);
  27.         }
  28.  
  29.         return;
  30.     }
  31. }
  32.  
Apr 17 '11 #1

✓ answered by Aimee Bailey

I'm betting this is probably because you still have the file open in read-only mode. A quick way to do what your trying to do could be:

Expand|Select|Wrap|Line Numbers
  1. public void ReplaceInFile(string file, string find, string replace)
  2. {
  3.     string buffer = File.ReadAllText(file);
  4.     buffer = buffer.Replace(find, replace);
  5.     File.WriteAllText(file, buffer);
  6. }
  7.  
However if you needed to use StreamReader and StreamWriter within a class, then this is probably what your looking for:

Expand|Select|Wrap|Line Numbers
  1. public void Replace(string find, string replace)
  2. {
  3.     string buffer = "";
  4.     using(FileStream fs = new FileStream(m_Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  5.     {
  6.         StreamReader sr = new StreamReader(fs);
  7.         buffer = sr.ReadToEnd();
  8.  
  9.         buffer = buffer.Replace(find, replace);
  10.  
  11.         fs.Position = 0;
  12.  
  13.         using(StreamWriter sw = new StreamWriter(fs))
  14.         {
  15.             sw.Write(buffer);
  16.             sw.Flush();
  17.             sw.Close();
  18.             sr = null;
  19.         }
  20.     }    
  21. }
  22.  
All the best.

Aimee.

1 7931
Aimee Bailey
197 Expert 100+
I'm betting this is probably because you still have the file open in read-only mode. A quick way to do what your trying to do could be:

Expand|Select|Wrap|Line Numbers
  1. public void ReplaceInFile(string file, string find, string replace)
  2. {
  3.     string buffer = File.ReadAllText(file);
  4.     buffer = buffer.Replace(find, replace);
  5.     File.WriteAllText(file, buffer);
  6. }
  7.  
However if you needed to use StreamReader and StreamWriter within a class, then this is probably what your looking for:

Expand|Select|Wrap|Line Numbers
  1. public void Replace(string find, string replace)
  2. {
  3.     string buffer = "";
  4.     using(FileStream fs = new FileStream(m_Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))
  5.     {
  6.         StreamReader sr = new StreamReader(fs);
  7.         buffer = sr.ReadToEnd();
  8.  
  9.         buffer = buffer.Replace(find, replace);
  10.  
  11.         fs.Position = 0;
  12.  
  13.         using(StreamWriter sw = new StreamWriter(fs))
  14.         {
  15.             sw.Write(buffer);
  16.             sw.Flush();
  17.             sw.Close();
  18.             sr = null;
  19.         }
  20.     }    
  21. }
  22.  
All the best.

Aimee.
Apr 20 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: gatekeeper | last post by:
Why could it be that i am able to use StreamWriter from a windows application but not from a web application? I am getting an unauthorizedaccessexception and the exception message is "Access to...
4
by: Joshua Campbell | last post by:
I am working on an application that needs to parse out data that has been copied into the clipboard. My first questions are how can I detect if the clipboard is empty, and how can I empty the...
3
by: Don | last post by:
I have a strange bug popping up every once in a while. I've got a section of code that executes these statements when working with a streamwriter: --- Start --- .... ' Close the...
1
by: Anthony Nystrom | last post by:
I am trying to pass a text stream from streamwriter class to a richtextbox, or textbox doesn't matter which one. I have the file write version working fine such as: x.streamwriter =...
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
3
by: Karan | last post by:
I have already imported system.io. Reader object is working fine: Dim sr As New StreamReader(fstream). Writer object Dim sw As New StreamWriter(fstream) object shows blue under line with type...
1
by: paul.hine | last post by:
Hello, I maintain an application that pulls data from a web service and writes it to an Excel sheet. Originally, the app used OleDb to write the Excel. Exports ran fine except that some text...
1
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via...
3
by: stumorgan | last post by:
I'm doing some USB communications in C# and am running into a minor annoyance. I'm using the Windows API CreateFile function to get a SafeFileHandle, which I then stuff into a FileStream and from...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
agi2029
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,...
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.