简体   繁体   中英

Write bytes of array into file, byte per byte by looping

I always use File.WriteAllBytes, but it's not working if the file is larger than my RAM available. Is it possible to write the file byte per byte and show the progress in a progress bar? If possible, can I do it in FileStream?

you can definitely do that.

I hope that following information will help you out.

http://msdn.microsoft.com/en-us/library/system.io.filestream.write(v=vs.110).aspx

if you provide some code that it is great to help you out.

Thanks.

You can use a BinaryWriter to write either byte by byte or, better, byte[] by byte[] (probably more efficient : truncate your bytes stream in reasonably sized chunks)

    using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
    {
        bytes[] nextBytes = GetNextBytes() // your logic to get what to write;
        writer.Write(nextBytes);
    }

As for the progress bar, this is a different question, but you could use a BackgroundWorker to report (how_many_bytes_you_wrote/total_bytes_to_write ) to a ProgressBar on your GUI.

Don't do that. Writing one byte at a time is not possible at the lower layers of I/O. If buffering is disabled, you'll end up with the following sequence of events:

  • Read a disk sector, if it isn't already in the file cache
  • Update one byte
  • Write the entire sector back to disk

When buffering is enabled, you're still making a bunch of extra kernel calls to update the file cache. Kernel calls are one of the more expensive things your program does. One is nowhere near as expensive as actual disk I/O, but 16000 are.

Instead, pick a block size that fits in memory, is a multiple of the disk cluster size and virtual member page size, and does a useful amount of work. Less than 64kB is just too much overhead for individual requests. More than 4 MB has pretty much no benefit. Also consider how many times to need to update the progress bar in order to make updates "smooth" (having more chunks than pixels in the progress bar width is not visible, and just wasted UI update processing in addition to the I/O requests).

Then, use the FileStream.Write overload that takes a byte array and writes all or part of it.

尝试使用这个BinaryWriter.Write访问: http : //msdn.microsoft.com/en-us/library/6tky7ax3%28v=vs.110%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM