2

I'm having an issue with StreamWriter and I just can't find what I'm doing wrong.

String line, new_line;
using (StreamReader sr = new StreamReader(txtFilePath.Text))
{
    using (StreamWriter sw = new StreamWriter(txtResultFolder.Text.ToString() + "\\" + "NEW_trimmed_file" + ".csv", true))
    {
        while ((line = sr.ReadLine()) != null)
        {
            new_line = line.TrimEnd();
            MessageBox.Show(new_line);
            sw.WriteLine(new_line);
        }
    }
}

I used the MessageBox.Show(new_line), just to be sure I have a value for the StreamWriter to write, but in the resulted file I cannot find anything. As an additional information I have a text which has empty spaces for each line (a lot of spaces) and I'm making another file with same lines as the first, but with no spaces. Any hints why the StreamWriter does not actually writes in the destination file?

Many thanks,

horHAY
  • 788
  • 2
  • 10
  • 23
BogdanM
  • 957
  • 4
  • 15
  • 32
  • Does it work if you specify the path manually? – Dion V. Apr 21 '15 at 06:58
  • What is the size of the written file? 0 bytes or 1 or 2 bytes? If it is 1 or 2 bytes I suspect that you write an empty line. – Ako Apr 21 '15 at 06:59
  • @Dion V:It's not relevant. When I ran it in debug, I can see the value loaded for the "line" string (from first file) and I can see the file named "NEW_trimmed_file.csv" from my location as well. But no lines are actually written. – BogdanM Apr 21 '15 at 07:01
  • Did you pause the debugger at the last '}' ? – Ako Apr 21 '15 at 07:02
  • @Ako: that was why i put the messagebox.show, just to be sure the line which I intend to write has some data. Which has because the dialog box shows it. The file has 0 bytes – BogdanM Apr 21 '15 at 07:02
  • @BogdanM Alright, that was not clear in your question, but if that's the case then refer to Yuval's answer. – Dion V. Apr 21 '15 at 07:08

2 Answers2

4

but in the resulted file I cannot find anything

If you want your StreamWriter to immediately write it's buffer before disposal, you need to call Flush():

string line, new_line;

using (StreamReader sr = new StreamReader(txtFilePath.Text))             
using (StreamWriter sw = new StreamWriter(txtResultFolder.Text.ToString() +
                                          "\\" + "NEW_trimmed_file" + ".csv", true))
{
   while ((line = sr.ReadLine()) != null)
   {
      new_line = line.TrimEnd();
      MessageBox.Show(new_line);
      sw.WriteLine(new_line);
   }
   sw.Flush();
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • You don't need to call `Flush()`. `StreamWriter` automatically flushes on `Dispose()`. – Jon Tirjan Apr 21 '15 at 07:11
  • 1
    @Yuval: Many, many thanks! It worked. Initially I've tried the sw.Flush() command just after sw.WriteLine(new_line), but with no effect, still an empty file. When used as you've written it worked perfectly. Many thanks again. – BogdanM Apr 21 '15 at 07:16
  • @Downvoter - I'd love to know what you find wrong with this answer. – Yuval Itzchakov Apr 21 '15 at 07:28
  • @Yuval - It's incorrect information. You don't need to call `Flush`. See posts from [Joel Coehoorn](http://stackoverflow.com/a/2417983/4698828) and [Jon Skeet](http://stackoverflow.com/a/1065196/4698828). – Jon Tirjan Apr 21 '15 at 07:42
  • You don't need to call `Flush` when you close the `StreamWriter`. This is wrong as @JonTirjan says. FWIW, I'm not the downvoter. – Sriram Sakthivel Apr 21 '15 at 12:53
0

There is a much shorter way to read and write from and to files. Just use File.ReadAllLines() and File.WriteAllLines()

var content = File.ReadAllLines(txtFilePath.Text)

File.WriteAllLines(txtResultFolder.Text.ToString(), content);

I am a little confused about of the naming of txtResultFolder. Is this path a folder? What is the exact content of this TextBox?

Michael Mairegger
  • 6,833
  • 28
  • 41
  • the code is just a sample from my winform application. The txtResultFolder is the destination folder chosen by the user. The destination for the StreamWriter is dynamically made. I cannot use ReadAllLines and WriteAllLines because my original file which I need to "trim" has around 22 millions lines, therefore only StreamWrite and StreamRead do work (tryed what you said but crashes the server) – BogdanM Apr 21 '15 at 07:06