byte[] bBuffer = new byte[0x10000];//create 64k buffer int nRealRead; do { nRealRead = yourFS.Read( bBuffer, 0, bBuffer.Length ); //Show data using "bBuffer ); } while( nRealRead == bBuffer.Length );
ReadByte 从文件中读取一个字节,并将读取位置提升一个字节。 Read 从流中读取字节块并将该数据写入给定缓冲区中。 using System; using System.IO;class FSRead { public static void Main() { //Create a file stream from an existing file. FileInfo fi=new FileInfo("c:\\csc.txt"); FileStream fs=fi.OpenRead();
//Read 100 bytes into an array from the specified file. int nBytes=100; byte[] ByteArray=new byte[nBytes]; int nBytesRead=fs.Read(ByteArray, 0, nBytes); Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString()); } }
"大于1MB的文件时,数据长度就会超出数组的最大长度"----为什么是1M?你自己设的吧?典型读法:FileStream filestm = new FileStream("path",FileMode.Open); byte[] buffer = new byte[(int)filestm.Length]; int iOffset = 0, iCount = buffer.Length; while (iCount > 0) { int m = filestm.Read(buffer,iOffset,iCount); if (m <= 0) break; iOffset += m; iCount -= m; }
Byte[] bt;
bt=File.ReadAllBytes(filepath);
int nRealRead;
do
{
nRealRead = yourFS.Read( bBuffer, 0, bBuffer.Length );
//Show data using "bBuffer );
}
while( nRealRead == bBuffer.Length );
Read 从流中读取字节块并将该数据写入给定缓冲区中。
using System;
using System.IO;class FSRead
{
public static void Main()
{
//Create a file stream from an existing file.
FileInfo fi=new FileInfo("c:\\csc.txt");
FileStream fs=fi.OpenRead();
//Read 100 bytes into an array from the specified file.
int nBytes=100;
byte[] ByteArray=new byte[nBytes];
int nBytesRead=fs.Read(ByteArray, 0, nBytes);
Console.WriteLine("{0} bytes have been read from the specified file.", nBytesRead.ToString());
}
}
byte[] buffer = new byte[(int)filestm.Length];
int iOffset = 0, iCount = buffer.Length;
while (iCount > 0)
{
int m = filestm.Read(buffer,iOffset,iCount);
if (m <= 0)
break;
iOffset += m;
iCount -= m;
}
但是当打开一个大于1MB的文件,就会提示超出了数组的范围(1000000)