什么是最可靠的方法来打开文件并读取内容 [英] What is the most robust way to open file and read content
本文介绍了什么是最可靠的方法来打开文件并读取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我通常这样做时,我要随时读取整个文件:
使用(VAR FILESTREAM = File.OpenRead(...))
使用(VAR读卡器=新的StreamReader(FILESTREAM))
VAR含量= reader.ReadToEnd();
任何更好/更快的方式?
解决方案
字符串内容= System.IO.File.ReadAllText(@你的文件路径);
如果你想获得行:
字符串[]行= System.IO.File.ReadAllLines(@你的文件路径);
如果你只想读线,直到得到一些行再使用了IEnumerable readlines方法:
在System.IO.File.ReadLines的foreach(串线(@你的文件路径)
{
如果(线== ...)
{
打破;
}
}
I usually do this when I have to read the whole file always:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
Any better/faster way?
解决方案
string content = System.IO.File.ReadAllText(@"your file path");
If you want to get lines:
string[] lines = System.IO.File.ReadAllLines(@"your file path");
if you want to only read lines until get to some line then use the IEnumerable ReadLines:
foreach(string line in System.IO.File.ReadLines(@"your file path")
{
if (line == ...)
{
break;
}
}
这篇关于什么是最可靠的方法来打开文件并读取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文