简体   繁体   English

c#使用memorystream而不是textwriter创建文件

[英]c# creating file using memorystream instead of textwriter

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. 我现在需要创建多个提取文件,并计划使用SharpZipLib(#ziplib)将它们压缩为单个.zip文件,并希望更改我的代码以在内存中创建文本文件,并使用它创建我的zip文件。 I think I should be creating/using a MemoryStream but can't figure out how to port my existing code. 我想我应该创建/使用MemoryStream但是无法弄清楚如何移植我现有的代码。

Thanks. 谢谢。

You could do: 你可以这样做:

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

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

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. 只有在特定进程或数据集的多个实现时,抽象才会创建模块化并减少代码。

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. 因此, TextWriter是可写流的良好抽象,但也考虑抽象出口类。

Eg 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. 通过现在提供独立于协议的界面,您将在以后更轻松地生活。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM