0

私はこのコードを使用しています

        string location1 = textBox2.Text;
        byte[] bytes = File.ReadAllBytes(location1);
        string text = (Convert.ToBase64String(bytes));
        richTextBox1.Text = text;

しかし、大きすぎるファイルを使用すると、メモリ不足の例外が発生します。

File.ReadAllBytesチャンクで使いたい。私は以下のようなコードを見ました

System.IO.FileStream fs = new System.IO.FileStream(textBox2.Text, System.IO.FileMode.Open);
byte[] buf = new byte[BUF_SIZE];
int bytesRead;

// Read the file one kilobyte at a time.
do
{
    bytesRead = fs.Read(buf, 0, BUF_SIZE);               
    // 'buf' contains the last 1024 bytes read of the file.

} while (bytesRead == BUF_SIZE);

fs.Close();

}

bytesReadしかし、実際にをテキストに変換するバイト配列に変換する方法がわかりません。

編集:答えが見つかりました。これがコードです!

 private void button1_Click(object sender, EventArgs e)
    {
        const int MAX_BUFFER = 2048;
        byte[] Buffer = new byte[MAX_BUFFER];
        int BytesRead;
        using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read))
            while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0)
            {
                string text = (Convert.ToBase64String(Buffer));
                textBox1.Text = text;

            }

}

テキスト形式の読み取り可能なバイトを変更するには、新しいバイトを作成して等しくします(Convert.FromBase64String(Text))。みんな、ありがとう!

4

5 に答える 5

1

bytesReadは、読み取られたバイト数です。

ここにいくつかのブロックの読み取りがあります

        var path = @"C:\Temp\file.blob";

        using (Stream f = new FileStream(path, FileMode.Open))
        {
            int offset = 0;
            long len = f.Length;
            byte[] buffer = new byte[len];

            int readLen = 100; // using chunks of 100 for default

            while (offset != len)
            {
                if (offset + readLen > len)
                {
                    readLen = (int) len - offset;
                }
                offset += f.Read(buffer, offset, readLen);
            }
        }

これで、にバイトがあり、buffer好きなように変換できます。

たとえば、「ストリームの使用」内:

            string result = string.Empty;

            foreach (byte b in buffer)
            {
                result += Convert.ToChar(b);
            }
于 2012-07-22T07:23:48.210 に答える
1
private void button1_Click(object sender, EventArgs e)
{
    const int MAX_BUFFER = 2048;
    byte[] Buffer = new byte[MAX_BUFFER];
    int BytesRead;
    using (System.IO.FileStream fileStream = new FileStream(textBox2.Text, FileMode.Open, FileAccess.Read))
        while ((BytesRead = fileStream.Read(Buffer, 0, MAX_BUFFER)) != 0)
        {
            string text = (Convert.ToBase64String(Buffer));
            textBox1.Text = text;

        }
}
于 2012-10-01T23:05:34.160 に答える
0

いいえ、からの戻り値Read()は読み取られたバイト数です。bufデータは、に渡すバイト配列にありますRead()。単にコピーアンドペーストするのではなく、コードを理解してから、なぜそれが機能しないのかを尋ねる必要があります。たとえそうだとしても、コメントすぐそこにそれを言います!

于 2012-07-22T02:10:59.867 に答える
0

ファイル構造によっては、メソッドStreamReaderを公開するを使用する方が簡単な場合がありますReadLine

using(var sr = new StreamReader(File.Open(textBox2.Text, FileMode.Open))
{
    while (sr.Peek() >= 0) 
    {
        Console.WriteLine(sr.ReadLine());
     }
}
于 2012-07-22T02:13:38.710 に答える
0

ファイルがテキストファイルの場合は、TextReaderを使用できます。

   string location1 = textBox2.Text;
    string text  = String.Empty;
    using (TextReader reader = File.OpenText(location1 ))
    {
           do
           {
           string line = reader.ReadLine();
               text+=line;
            }
            while(line!=null)

    }
于 2012-07-22T07:06:55.847 に答える