C#I / O - System.IO.File和StreamWriter / StreamReader之间的区别 [英] C# I/O - Difference between System.IO.File and StreamWriter/StreamReader

查看:113
本文介绍了C#I / O - System.IO.File和StreamWriter / StreamReader之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我只对处理文本文件感兴趣,与StreamWriter相比,System.IO.File方法有什么特别的优缺点?



有没有性能因素涉及?什么是基本的区别,以及哪些应该用在什么情况下?

还有一个问题,如果我想把一个文件的内容读入一个字符串中运行一个LINQ查询,这是最好的?

解决方案

有一些有趣的历史背后的文件看似重复的方法类。它是在对.NET的预发布版本进行可用性研究之后出现的。他们要求一组有经验的程序员编写代码来操作文件。他们以前从来没有接触过.NET,只是有文档工作。成功率是0%。

是的,这是有区别的。当你试图读取一个千兆字节或更多的文件时,你会发现。这是32位版本的保证崩溃。没有这样的逐行读取StreamReader的问题,它将使用很少的内存。这取决于你的程序的其他部分是做什么的,但是试着把方便的方法限制在不超过几兆字节的文件上。


Assuming I am interested only in dealing with text files, what specific advantages or disadvantages does System.IO.File methods provide when compared to StreamWriter?

Are there any performance factors involved? What is the basic difference, and which of these should be used in what cases?

One more question, If i want to read the contents of a file into a string and run a LINQ query over it, which is best?

解决方案

There's a bit of interesting history behind the seemingly duplicate methods in the File class. It came about after a usability study on a pre-release version of .NET. They asked a group of experienced programmers to write code to manipulate files. They were never exposed to .NET before and just had the docs to work from. The success rate was 0%.

Yes, there's a difference. You'll find out when you try to read a file that's a gigabyte or more. That's a guaranteed crash on the 32-bit version. No such problem with a StreamReader that reads line-by-line, it will use very little memory. It depends on what the rest of your program does but try to limit the convenience method to files no larger than, say, a couple of megabytes.

这篇关于C#I / O - System.IO.File和StreamWriter / StreamReader之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆