c#使用内存流而不是textwriter创建文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/781583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# creating file using memorystream instead of textwriter
提问by tmcallaghan
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.
我现在需要创建多个提取文件,并计划使用 SharpZipLib (#ziplib) 将它们压缩成单个 .zip 文件,并希望更改我的代码以在“内存中”创建文本文件并使用它来创建我的 zip 文件。我想我应该创建/使用 MemoryStream 但不知道如何移植我现有的代码。
Thanks.
谢谢。
采纳答案by Peter Lillevold
You could do:
你可以这样做:
MemoryStream memoryStream = new MemoryStream();
TextWriter tw = new StreamWriter(memoryStream);
tw.WriteLine("ref\tACCOUNT\tbatch\tDEBIT\tCREDIT\tDesc");
回答by Groo
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.
我还建议现在是尝试解耦应用程序部分的好时机,以便您将来可以更改其中的部分内容。所以, aTextWriter
是可写流的一个很好的抽象,但也要考虑抽象你的导出类。
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.
通过现在提供独立于协议的接口,您以后的生活会更轻松。
回答by Nathaniel
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 practicea good abstraction technique that's fine, but there are infiniteabstraction opportunities when writing any significant amount of code. Abstraction creates modularity and reduces code onlywhen there are multiple implementations of a particular process or data set.
如果您想添加导出器来练习一种良好的抽象技术,那很好,但是在编写大量代码时有无限的抽象机会。只有当特定流程或数据集有多个实现时,抽象才能创建模块化并减少代码。