StreamWriter, need to appendtext and not overwrite af file

M

Mads Westen

Hi,

I'm parsing some XML files, and converting them to CSV.
I got x number of XML files in a directory, these files do I need to be
converted and written to the same CSV file.
My problem is that my csv file only contains the data from the last XML file
parsed...

I got the following code for creating of my StreamWriter:
public OrderFileCreator(string fileName)
{
this.fileName = fileName;
if(File.Exists(this.fileName))
{
fl = File.OpenWrite(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
if(!File.Exists(this.fileName))
{
fl = File.Create(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
}


Best regards
Mads
 
M

Mads Westen

Hi, Vadym,

I finally googled my way to another solution.
I used the following:
if(File.Exists(this.fileName))
{
fl = new FileStream(this.fileName, FileMode.Append);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
if(!File.Exists(this.fileName))
{
fl = File.Create(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
That worked fine.

I'm sure your solution would have worked to.
In my case, I don't care what end of the file i write to. But if that was a
issue, then your solution would come in handy.

Thaks!

Best regards
Mads
 
H

Helge Jensen

Mads said:
if(File.Exists(this.fileName))
{
fl = new FileStream(this.fileName, FileMode.Append);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}
if(!File.Exists(this.fileName))
{
fl = File.Create(this.fileName);
fs = new StreamWriter(fl, Encoding.GetEncoding( 850 ));
}

What's wrong with simply:

Stream s = File.Open(fileName, FileMode.Append);
StreamWriter sw = new StreamWriter(s, enc);

BTW, you should probably be "using" s and sw, or have some other method
of ensuring that they get closed.
 
V

Vadym Stetsyak

HJ> What's wrong with simply:

HJ> Stream s = File.Open(fileName, FileMode.Append);
HJ> StreamWriter sw = new StreamWriter(s, enc);

If you plan only to write to thele this is okay, because
File.Open(fileName, FileMode.Append) - opens file with FileAccess.Write.

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
H

Helge Jensen

Vadym said:
HJ> What's wrong with simply:

HJ> Stream s = File.Open(fileName, FileMode.Append);
HJ> StreamWriter sw = new StreamWriter(s, enc);

If you plan only to write to thele this is okay, because
File.Open(fileName, FileMode.Append) - opens file with FileAccess.Write.

I think you can assume that only writes are to be done, since the
original example did new FileStream(name, FileMode.Append) -- which i
would believe is equivalent.

I just found it odd to check that the file exists first. It complicates
the code and introduces a race condition, probably not a bad one, but
still...
 

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