471,316 Members | 988 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Using streamwriter to write CSV, but empty files

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
Dec 6 '05 #1
3 15190
Make sure you flush and then close the StreamWriter...

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

JP

Dec 6 '05 #2
..Close() is all that is needed.

--

Derek Davis
dd******@gmail.com

<jo*********@topscene.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Make sure you flush and then close the StreamWriter...

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

JP

Dec 7 '05 #3
Thanks alot,
Works flawlessly!

Thumbs up from here :)

Best regards
Mads

"carion1" <dd******@gmail.com> skrev i en meddelelse
news:OU****************@TK2MSFTNGP11.phx.gbl...
.Close() is all that is needed.

--

Derek Davis
dd******@gmail.com

<jo*********@topscene.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Make sure you flush and then close the StreamWriter...

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

JP


Dec 7 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Angel | last post: by
reply views Thread by rosydwin | last post: by

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.