Solved

Create file with a specific size..

Posted on 2003-11-05
4
380 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
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
Good suggestion Avenger
0
 

Author Comment

by:jooon
Comment Utility
Thank you both!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now