簡體   English   中英

讀取文件流閱讀器

[英]Read file stream reader

我的錯誤是什么,因為我無法在互聯網上找到與我正在做的事情相符的例子,或者至少我不確定它是否存在?

我遇到的問題是它不喜歡

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();
}

失蹤 ”)”。 嘗試:

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

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

我會做一些不同的事情。

第一個,是你應該使用FileStreamusing 但實際上,如果您只是想讀取文本文件中的行, 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
}

如果您嘗試將整個輸入文件轉換為十六進制值,請告訴我們。 我現在只是逐行假設。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM