读取大文件(以字节为单位) [英] Reading large files in bytes

查看:93
本文介绍了读取大文件(以字节为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据建议,我已经开始执行以下操作:

As per the suggestion, I have started to implement the following:

 private string Reading (string filePath)
    {
        byte[] buffer = new byte[100000];

        FileStream strm = new FileStream(filePath, FileMode.Open, FileAccess.Read,
        FileShare.Read, 1024, FileOptions.Asynchronous);

        // Make the asynchronous call
        IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, new 
        AsyncCallback(CompleteRead), strm);

    }

       private void CompleteRead(IAsyncResult result)
    {
        FileStream strm = (FileStream)result.AsyncState;

        strm.Close();
    }

我该如何真正返回已读取的数据?

How do I go about actually returning the data that I've read?

推荐答案

public static byte[] myarray;

static void Main(string[] args)
{

    FileStream strm = new FileStream(@"some.txt", FileMode.Open, FileAccess.Read,
        FileShare.Read, 1024, FileOptions.Asynchronous);

    myarray = new byte[strm.Length];
    IAsyncResult result = strm.BeginRead(myarray, 0, myarray.Length, new
    AsyncCallback(CompleteRead),strm );
    Console.ReadKey();
}

    private static void CompleteRead(IAsyncResult result)
    {
          FileStream strm = (FileStream)result.AsyncState;
          int size = strm.EndRead(result);

          strm.Close();
          //this is an example how to read data.
          Console.WriteLine(BitConverter.ToString(myarray, 0, size));
    }

它不应读为"Random",它的读取顺序相同,但以防万一,请尝试以下操作:

It should not read "Random",it reads in the same order but just in case try this:

Console.WriteLine(Encoding.ASCII.GetString(myarray));

这篇关于读取大文件(以字节为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆