34

I have an application that is currently creating a text file to import into an accounting application. It is using the following code to create the file and write lines to it:

    TextWriter tw = new StreamWriter(ExtractFileName);

    tw.WriteLine("ref\tACCOUNT\tbatch\tDEBIT\tCREDIT\tDesc");

I now need to create multiple extract files and plan on compressing them into a single .zip file using SharpZipLib (#ziplib) and want to change my code to do the text file creation "in memory" and using that to create my zip file. I think I should be creating/using a MemoryStream but can't figure out how to port my existing code.

Thanks.

3 Answers 3

97

You could do:

MemoryStream  memoryStream = new MemoryStream();
TextWriter tw = new StreamWriter(memoryStream);

tw.WriteLine("ref\tACCOUNT\tbatch\tDEBIT\tCREDIT\tDesc");
Sign up to request clarification or add additional context in comments.

4 Comments

Perfect, thanks for the quick response. My .zip file is now created.
Thats great, glad to be of assistance :)
It is usually recommended to use the using with memory stream to ensure disposal.
@ppumkin - agree, but: 1) my sample was intended to show how to use MemoryStream with StreamWriter, not lecture on resource disposal. 2) when and how the stream will be disposed largely depends on the rest of the OP's code. Always ensure disposal, though not always can you use using.
9

Don't create unnecessary abstraction. While the exporter class is cool it only adds value when you have more than one export strategy. Otherwise it is clutter that distracts from the core purpose of your code.

If you want to add the exporter to practice a good abstraction technique that's fine, but there are infinite abstraction opportunities when writing any significant amount of code. Abstraction creates modularity and reduces code only when there are multiple implementations of a particular process or data set.

1 Comment

@MarcL. CTHULHU FHTAGN!
6

I would also suggest that this is a good time to try to decouple parts of your app, so that you can change parts of it in the future. So, a TextWriter is a good abstraction for a writable stream, but consider abstracting your export class also.

E.g. now you want to do it like this:

MemoryStream  memoryStream = new MemoryStream();
TextWriter tw = new StreamWriter(memoryStream);

// tab-delimited export
IExporter exporter = new DelimiterExport(data, tw, "\t"); 
exporter.Export();

so that you can easily change it to:

// csv file (stands for "comma separated value", but you should actually
// use a culture-specific list separator instead)
var separator = Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator;
IExporter exporter = new DelimiterExport(data, tw, separator);

or any other implementation:

// excel export
IExporter exporter = new ExcelExport(data, tw);

By providing a protocol independent interface now, you will make your life easier later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.