Solved

Create file with a specific size..

Posted on 2003-11-05
4
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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