![](/img/trans.png)
[英]I am unable to read content from .txt file using StreamReader class of c#
[英]C#: Using StreamReader to read line from txt file, but Peek() return -1 even there are a lot of lines left
我使用StreamReader的Peek()方法来检查是否需要处理更多行。 我的文件中有超过1000行,但Peek()在到达#750行时突然返回-1。 我查了一下,但#750和#751之间似乎没有区别。 即使我删除了#750和751行,它仍会在其他行中分解。
以下是我的代码供您参考:
try
{
String ftpserver = ftp + filename;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpserver));
reqFTP.UsePassive = false;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
response = (FtpWebResponse)reqFTP.GetResponse();
stream = response.GetResponseStream();
reader = new StreamReader(stream, ConfigHelper.MyEncoding);
while (reader.Peek() > -1)
{
string x = reader.ReadLine();
if (x != null)
{
//.......
}
}
}
catch (Exception ex)
{
}
finally
{
if (reader != null)
reader.Close();
if (response != null)
response.Close();
}
我尝试了while ((x = reader.ReadLine()) != null)
,但是“无法访问已处置的对象”的例外被抛出。
最后我通过使用: while (stream.CanRead && (x = reader.ReadLine()) != null)
虽然它没有解释发生了什么,但我个人避免使用Peek
。 我用的是:
string line;
while ((line = reader.ReadLine()) != null)
{
// Use the line
}
这样你就只能在一个地方读书 。 它在某种程度上比检查你是否可以阅读,然后阅读更加明智。
您还可以编写一个方法来从TextReader
(或从Func<TextReader>
或文件名)创建IEnumerable<string>
,这可以使所有这些更加愉快。 如果您只是在阅读文件并且使用的是.NET 4,则File.ReadLines
已经内置。
编辑:这是您从StreamReader.Peek
的文档获得-1的一个原因:
表示要读取的下一个字符的整数,如果没有要读取的字符或者流不支持搜索,则返回-1。
你的流是否支持寻求?
我不确定为什么Peek方法在你的情况下返回-1,但通常使用StreamReader类读取行到文件末尾的方法是重复调用ReadLine方法直到返回null
:
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
你需要偷看吗? 你是否正在跳过某些行? 如果你想阅读所有行使用它。
while(!sr.EndOfStream)
{
//Do stuff
}
如MSDN中所述, Peek Method不仅在到达流的末尾时返回-1,而且在内部错误的情况下:
Peek方法返回一个整数值,以确定是否发生了文件末尾或其他错误 。 这允许用户在将其转换为Char类型之前首先检查返回值是否为-1。
也许在SQL命令中检查错误的数据转换,我认为这个方法也应该有效!
尝试与需要身份验证的应用程序进行交互时遇到了类似的问题。 当遇到时髦的字符(unicode字符?)时,Peek()会返回-1,而ReadLine()也不可靠,最终会锁定我的应用程序,因为它似乎是标准流未关闭的过程。
使用Read()方法是我能确保获得所有行和字符的唯一方法。 此外,使用Process的ErrorDataReceived或OutputDataReceived事件处理程序也证明了UNRELIABLE(缺少行)。 下面是我如何解决我的问题,并保证收到所有行和字符:
process.Start();
var stdOutput = process.StandardOutput;
StringBuilder fullMessage = new StringBuilder();
while (true)
{
var character = (char)stdOutput.Read();
fullMessage.Append(character);
//Print Out All Characters
Console.Write(character);
if (fullMessage.ToString().EndsWith("Enter Password: "))
{
//Submit Password To Application
using(StreamWriter writer = process.StandardInput){
writer.Write("somepassword");
writer.Flush();
}
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.