如何通过特定的行分隔符读取文本文件?
使用流读取器读取文本文件。
using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
{
string line = sr.ReadLine();
}
我想强制行分隔符应该是 \n
而不是 \r
。那么我该怎么做呢?
Reading a text file using streamreader.
using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
{
string line = sr.ReadLine();
}
I want to force that line delimiter should be \n
not \r
. So how can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我会实现类似乔治的答案,但作为一种扩展方法,可以避免一次加载整个文件(未经测试,但类似这样):
然后可以像这样使用:
I would implement something like George's answer, but as an extension method that avoids loading the whole file at once (not tested, but something like this):
Which could then be used like:
我喜欢@Pete 给出的答案。我只想提交一个小小的修改。这将允许您传递字符串分隔符而不仅仅是单个字符:
I loved the answer @Pete gave. I would just like to submit a slight modification. This will allow you to pass a string delimiter instead of just a single character:
我需要一个读取到“\r\n”并且不会在“\n”处停止的解决方案。 jp1980 的解决方案有效,但处理大文件时速度极慢。因此,我将 Mike Sackton 的解决方案转换为读取,直到找到指定的字符串。
它的名字是这样的......
I needed a solution that reads until "\r\n", and does not stop at "\n". jp1980's solution worked, but was extremely slow on a large file. So, I converted Mike Sackton's solution to read until a specified string is found.
And it is called like this...
根据文档:
http://msdn.microsoft。 com/en-us/library/system.io.streamreader.readline.aspx
默认情况下,StreamReader ReadLine 方法将通过 \n 或 \r 识别一行
According to the documentation:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx
By default the StreamReader ReadLine method will recognise a line by both/either \n or \r
这是 sovemp 答案的改进。抱歉,我本来想发表评论,尽管我的声誉不允许我这样做。此改进解决了 2 个问题:
删除第一个不想要的“\r”。
当流中的最后一个字符等于分隔符时,函数将
错误地返回包含分隔符的字符串。
This is an improvement of sovemp answer. Sorry I would have liked to comment, although my reputation doesn't allow me to do so. This improvement addresses 2 issues:
delete the first "\r" which is not intended.
when last characters in stream equals delimiter, function would
wrongly return string including delimiters.
您要么必须自己逐字节解析流并处理分割,要么需要使用默认的 ReadLine 行为,该行为在 /r、/n 或 /r/n 上分割。
如果你想逐字节解析流,我会使用类似以下扩展方法:
You either have to parse the stream byte-by-byte yourself and handle the split, or you need to use the default ReadLine behavior which splits on /r, /n, or /r/n.
If you want to parse the stream byte-by-byte, I'd use something like the following extension method:
即使你说“使用 StreamReader”,因为你也说“我的情况,文件可以有大量记录......”,我建议尝试 SSIS。它非常适合您想要做的事情。您可以处理非常大的文件并轻松指定行/列分隔符。
Even though you said "Using StreamReader", since you also said "I my case, file can have tons of records...", I would recommend trying SSIS. It's perfect for what you're trying to do. You can process very large file and specify the line/column delimiters easily.
此代码片段将从文件中读取一行,直到遇到“\n”。
因为此代码逐字符读取,所以它可以处理任何长度的文件,而不受可用内存的限制。
This code snippet will read a line from a file until it encounters "\n".
Because this code reads character by character it will work with a file of any length without being constrained by available memory.