且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

读取大文件(以字节为单位)

更新时间:2023-01-05 14:29:56

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