如何生成一定长度的字符串以插入文件中以满足文件大小标准? [英] How to generate string of a certain length to insert into a file to meet a file size criteria?

查看:51
本文介绍了如何生成一定长度的字符串以插入文件中以满足文件大小标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试一些有关文件大小的负载问题.我有一个用C#编写的Windows应用程序,它将自动生成文件.我知道每个文件的大小,例如.100KB,以及要生成多少个文件.我需要帮助的是如何生成小于或等于所需文件大小的字符串.

I have a requirement to test some load issues with regards to file size. I have a windows application written in C# which will automatically generate the files. I know the size of each file, ex. 100KB, and how many files to generate. What I need help with is how to generate a string less than or equal to the required file size.

伪代码:

long fileSizeInKB = (1024 * 100); //100KB
int numberOfFiles = 5;

for(var i = 0; i < numberOfFiles - 1; i++) {
     var dataSize = fileSizeInKB;
     var buffer = new byte[dataSize];
     using (var fs = new FileStream(File, FileMode.Create, FileAccess.Write)) {

     }
}

推荐答案

对于字符串,您总是可以使用构造器,该构造器使用 char 并多次重复该字符:

You can always use the a constructor for string which takes a char and a number of times you want that character repeated:

string myString = new string('*', 5000);

这将为您提供5000颗星星的字符串-根据您的需要进行调整.

This gives you a string of 5000 stars - tweak to your needs.

这篇关于如何生成一定长度的字符串以插入文件中以满足文件大小标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆