如何读取大文件(高达4 GB)并转换为字节? [英] How to read large file (up 4 GB) and convert to byte?

查看:85
本文介绍了如何读取大文件(高达4 GB)并转换为字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我想读取高音量的文件和变量Byte [] put。



下面的代码没有工作

byte [] bytes = System.IO.File.ReadAllBytes(filename);



OR



private byte [] StreamFile(string filename)

{

FileStream fs = new FileStream(filename,FileMode.Open,FileAccess.Read) ;



//创建一个文件流长度的字节数组

byte [] ImageData = new byte [fs.Length];



//从流中读取字节块到字节数组

fs.Read(ImageData,0,System.Convert.ToInt32(fs。长度));



//关闭文件流

fs.Close();

返回ImageData ; //返回字节数据

}







Stream dest = ...

使用(Stream source = File.OpenRead(path)){

byte [] buffer = new byte [2048];

int bytesRead;

while((bytesRead = source.Read(buffer,0,buffer.Length))> 0){

dest.Write(buffer, 0,bytesRead);

}

}



我尝试过:



如何读取大文件(高达4 GB)并转换为字节?

Hi.
I want to read files with high volume and the a variable Byte[] put.

Below code does not work
byte[] bytes = System.IO.File.ReadAllBytes(filename);

OR

private byte [] StreamFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);

// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];

//Read block of bytes from stream into the byte array
fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs.Close();
return ImageData; //return the byte data
}

OR

Stream dest = ...
using(Stream source = File.OpenRead(path)) {
byte[] buffer = new byte[2048];
int bytesRead;
while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {
dest.Write(buffer, 0, bytesRead);
}
}

What I have tried:

How To Read Large File (Up 4 GB) And Convert To Byte?

推荐答案

基本上,你不能,不能使用字节数组。

你可以为大于2GB的物体做到这一点,如果你是:

1)在64位模式下运行64位系统。 32位应用程序无法处理大于2GB的内存。

2)运行.NET Framework V4.5或更高版本。



3)有在你的app.config中设置gcAllowVeryLargeObjects: gcAllowVeryLargeObjects Element [ ^ ]

但是......数组索引器仍然限于一个整数 - 所以你不能用字节数组来做这个,因为你的数组索引太大了。

你可以做到有一个流(允许〜8TB)但没有数组。



坦率地说,如果你需要加载一个大的内存文件,你可能需要看看你的应用程序是如何工作的,因为你的基本算法可能存在一些问题...
Basically, you can't, not with a byte array.
You can do it for objects larger than 2GB, if you are:
1) Running in 64bit mode on a 64 bit system. 32bit apps cannot address memory bigger than 2GB.
2) Are running .NET Framework V4.5 or greater.
And
3) Have set gcAllowVeryLargeObjects in your app.config: gcAllowVeryLargeObjects Element[^]
But ... array indexers are still limited to an integer - so you can't do this with byte arrays as your array index would be too big.
You can do it with a stream (~8TB is allowed) but not with an array.

And frankly, if you need to load a single file that large into memory, you probably need to look at how your app is working anyway because there is probably something very wrong with your basic algorithm...


一个例子是查看和下载


这篇关于如何读取大文件(高达4 GB)并转换为字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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