C# 文件流读/写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11872972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:35:31  来源:igfitidea点击:

FileStream Read/Write

c#visual-studio-2010file-iostream

提问by Bubo

Ok, So I am writing another program for the purpose of manipulating binary files. This program is importing a file larger than anything I have had to manipulate before, about 12K.

好的,所以我正在编写另一个程序来操作二进制文件。该程序正在导入一个比我之前必须操作的任何文件都大的文件,大约 12K。

I'm curious as to how the Stream.read command works....I know that this sounds elementary, but how can I tell that the file has been read completely so that I can begin manipulating it, as of right now I have this code...

我很好奇 Stream.read 命令是如何工作的......我知道这听起来很基本,但是我怎么知道文件已被完全读取,以便我可以开始操作它,截至目前我有这段代码...

// Opens a stream to the path chosen in the open file dialog
using (FileStream stream = new FileStream(chosenFile, FileMode.Open, FileAccess.Read))                     
{
    size = (int)stream.Length; // Returns the length of the file
    data = new byte[size]; // Initializes and array in which to store the file
    stream.Read(data, 0, size); // Begins to read from the constructed stream
    progressBar1.Maximum = size;

    while (byteCounter < size)
    {
        int i = data[byteCounter];

        byteCounter++;
        progressBar1.Increment(1);
    } 
}

I Understand that this is very very simple, but can someone explain to me how stream.Read works, does it store everything into the byte array "data" and then I can manipulate that as I see fit, or do I have to manipulate the file as it is being read. Again I apologize if this is elementary, all thoughts are appreciated

我知道这非常非常简单,但是有人可以向我解释 stream.Read 是如何工作的,它是否将所有内容都存储到字节数组“数据”中,然后我可以按照我认为合适的方式操作它,或者我是否必须操作正在读取的文件。如果这是基本的,我再次道歉,感谢所有的想法

采纳答案by Steve

This line

这条线

stream.Read(data, 0, size); 

reads everything from the stream and stores the file content in your byte array
You can start working immediately on the array.

从流中读取所有内容并将文件内容存储在您的字节数组中
您可以立即开始处理该数组。

See FileStream.Readdocs on MSDN

请参阅MSDN 上的FileStream.Read文档

Your code reads the length of the file, allocate a byte array of the correct size, then reads everything in a single shot.
Of course this method is not viable if your file is really big.
(and the definition of 'big' could be different compared to the available memory). In that case the approach used is to read chunks of the file, processing and looping till all the bytes are read.

您的代码读取文件的长度,分配正确大小的字节数组,然后一次性读取所有内容。
当然,如果您的文件很大,这种方法是不可行的。
(与可用内存相比,“大”的定义可能不同)。在这种情况下,使用的方法是读取文件的块,处理和循环直到读取所有字节。

However, DotNet has specialized classes for reading and writing binary files.
See the documentation on BinaryReader

但是,DotNet 有专门的类用于读取和写入二进制文件。
请参阅有关BinaryReader的文档

回答by Austin Salonen

This doesn't exactly answer your question as to how Stream.Read worksbut does illuminate the fact that what you want already exists in .Net.

这并不能完全回答你关于 Stream.Read 如何工作的问题,但确实说明了你想要的东西已经存在于 .Net 中的事实。

File.ReadAllByteswould work without issue for a 12K file.

File.ReadAllBytes对于 12K 文件可以正常工作。

byte[] content = File.ReadAllBytes("path");