读取文件的流阅读器

4

我做错了什么,为什么在互联网上找不到与我所做的相匹配的示例,或者至少我不确定是否有相匹配的示例?

我遇到的问题是它不喜欢。

hexIn = fileStream.Read()

代码:

FileStream fileStream = new FileStream(fileDirectory, FileMode.Open, FileAccess.Read);
String s;

try
{
    for (int i = 0; (hexIn = fileStream.Read() != -1; i++)
    {
        s = hexIn.ToString("X2");
        //The rest of the code
    }
}
finally
{
    fileStream.Close();
}

4
“它不喜欢”并不是非常详细的描述。您遇到的确切问题是什么? - John Saunders
2个回答

7

缺少")"。请尝试:

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
    String line;

    while ((line = sr.ReadLine()) != null)
    {
        s=...
    }
}

2

有几件事情我会做得不同。

首先,你应该使用 FileStream 并加上 using。但实际上,如果你只是想读取文本文件中的行,那么 StreamReader 就足够了:

try
{
    using (StreamReader sr = new StreamReader("TestFile.txt"))
    {
        String line;

        while ((line = sr.ReadLine()) != null)
        {
            // convert line to Hex and then format with .ToString("X2")
        }
    }
}
catch
{
    // handle error
}

如果您想将整个输入文件转换为十六进制值,请告诉我们。现在我只能逐行进行假设。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接