Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create file with a specific size..

Posted on 2003-11-05
4
Medium Priority
?
402 Views
Last Modified: 2007-12-19
Hi, I want to create a file with a specific size. I wrote this code:

string FILE_NAME = @"C:\test.data";
FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
BinaryWriter b = new BinaryWriter(fs);

const uint _byte = 1024;
ulong MB = (_byte * _byte) * UInt64.Parse(textBox1.Text.ToString()); //for example: 30
byte[] buffer = new byte[MB];
b.Write(buffer);
fs.Close();
b.Close();

..and I have some questions:
Is this a bad way to do it?
How big file can I create this way?
Can I optimize the speed?

Thanks!
0
Comment
Question by:jooon
  • 2
4 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 9692068
The file can be as large as you want it.  I would recommend that whatever size you decide to make it, you write it in blocks of a fixed size multiple times until it's the size desired.

Is this a bad way to do it?  
That's a question for the ages.  If it accomplishes the goal in a not too timely fashion then it's a good way to do it.

Can I optimize the speed?
That will depend on specifically what you are doing.  With the existing code it doesn't appear that there is any optimization left, but if you are doing other things as well there may be some optimization.  Also the block size that you use to write to the file.  A large enough number to not use too much RAM, but a small enough number to work on any machine that you plan on deploying the application to.  Depending on what the total file size is supposed to be I would break it into chunks not greater than 1 MB for each write.  There's probably some optimization to going even smaller (256K or 64K) depending on what can get through the Cache quickly.
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 150 total points
ID: 9692673
ulong MB = (_byte * _byte) * UInt64.Parse(textBox1.Text.ToString()); //for example: 30
byte[] buffer = new byte[MB];

This is not very effective as it creates first a large empty buffer in memory and then writes it to the file. This can also limit the size of file you can create. So follow the advice of SRigney and make it write in several blocks of let's say 100 Kb or 1 Mb.

In order to optimize the speed, use a BufferedStream instead of the BinaryWriter. Then you can use a very small buffer so you can create a loop like the one that follows and still have a very good speed as the BufferedStream will make disk operations only when it's buffer is full. This will change performance significantly if you use files bigger than a couple of Mbs.

ulong MB = UInt64.Parse(textBox1.Text.ToString()); //for example: 30
byte[] buffer = new byte[1024];
for (int i = 0; i < MB; i++)
  for (int j = 0; j < 1024; j++)
    b.Write (buffer);
0
 
LVL 15

Expert Comment

by:SRigney
ID: 9693653
Good suggestion Avenger
0
 

Author Comment

by:jooon
ID: 9695144
Thank you both!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question