Solved

Create file with a specific size..

Posted on 2003-11-05
4
399 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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