Using streamwriter to write CSV, but empty files


M

Mads Westen

Hi,

I'm making some changes to my program, because I need to write with Codepage
850.
Before I used FileStream, but I found code to use with StreamWriter instead.

My problem is, that I don't get any output when writing with StreamWriter,
but only with FileStream.

I got StreamWriter to work in another class, where I only have 1 Writeline
method.
In my code, I'm trying to debug using both FileStream and StreamWriter to
write the file. I have tried to outcomment the fs.write, but without luck.

My files are created fine, but the're just empty!

What am I doing wrong? Souce code here:

using System;
using System.Text;
using System.IO;

namespace SAEXML2CSV
{

public class OrderFileCreator
{
private string fileName = "";
private FileStream fs;
private StreamWriter sw;

{
this.fileName = fileName;

//opret ny fil
if(File.Exists(this.fileName))
Console.WriteLine("\nFilen fandtes allerede, den gamle fil er nu
overskrevet");
File.Delete(this.fileName);
if(!File.Exists(this.fileName))
{
fs = File.Create(this.fileName);
sw = new StreamWriter(fs, Encoding.GetEncoding( 850 ));
}
}

public void writeLine(string val0, string val1, string val2, string val3,
string val4)
{
string linie =
"\""+val0+"\",\""+val1+"\",\""+val2+"\",\""+val3+"\",\""+val4+"\"\n";
Byte[] info = new UTF8Encoding(true).GetBytes(linie);
this.fs.Write(info, 0, info.Length);
this.sw.WriteLine(linie);
}


public void writeOrderHead(string val1, string val2, string val3, string
val4, string val5)
{
string linie =
"\""+val1+"\",\""+val2+"\",\""+val3+"\",\""+val4+"\",\""+val5+"\"\n";
Byte[] info = new UTF8Encoding(true).GetBytes(linie);
this.fs.Write(info, 0, info.Length);
this.sw.WriteLine(linie);
}

}
}
Best regards

Mads
 
Ad

Advertisements

J

joey.powell

Make sure you flush and then close the StreamWriter...

sw.Flush();
sw.Close();

JP
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top