如何生成一定长度的字符串插入文件以满足文件大小的要求?

时间:2022-01-02 07:25:42

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.

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

pseudo code:

伪代码:

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)) {

     }
}

4 个解决方案

#1


218  

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);

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

这将为您提供一串5000颗星星——根据您的需要进行调整。

#2


11  

Easiest way would be following code:

最简单的方法是:

var content = new string('A', fileSizeInKB);

Now you've got a string with as many A as required.

现在你有了一个字符串,它有足够多的a。

To fill it with Lorem Ipsum or some other repeating string build something like the following pseudocode:

使用Lorem Ipsum或其他重复字符串填充它,构建如下伪代码:

string contentString = "Lorem Ipsum...";
for (int i = 0; i < fileSizeInKB / contentString.Length; i++)
  //write contentString to file

if (fileSizeInKB % contentString.Length > 0)
  // write remaining substring of contentString to file

Edit: If you're saving in Unicode you may need to half the filesize count because unicode uses two bytes per character if I remember correctly.

编辑:如果你保存的是Unicode,那么你可能需要一半的filesize计数,因为如果我没有记错的话,Unicode的每个字符使用两个字节。

#3


0  

There are so many variations on how you can do this. One would be, fill the file with a bunch of chars. You need 100KB? No problem.. 100 * 1024 * 8 = 819200 bits. A single char is 16 bits. 819200 / 16 = 51200. You need to stick 51,200 chars into a file. But consider that a file may have additional header/meta data, so you may need to account for that and decrease the number of chars to write to file.

如何做到这一点有很多不同的方法。一种是,用一堆字符填充文件。你需要100 kb ?没有问题. .100 * 1024 * 8 = 819200位。一个字符是16位。819200 / 16 = 51200。您需要将51200个字符插入到一个文件中。但是考虑到一个文件可能有额外的头/元数据,所以您可能需要考虑到这一点,并减少要写入文件的字符数。

#4


0  

As a partial answer to your question I recently created a portable WPF app that easily creates 'junk' files of almost any size: https://github.com/webmooch/FileCreator

作为对你问题的部分回答,我最近创建了一个便携式WPF应用程序,可以轻松创建几乎任何大小的“垃圾”文件:https://github.com/webmooch/FileCreator

#1


218  

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);

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

这将为您提供一串5000颗星星——根据您的需要进行调整。

#2


11  

Easiest way would be following code:

最简单的方法是:

var content = new string('A', fileSizeInKB);

Now you've got a string with as many A as required.

现在你有了一个字符串,它有足够多的a。

To fill it with Lorem Ipsum or some other repeating string build something like the following pseudocode:

使用Lorem Ipsum或其他重复字符串填充它,构建如下伪代码:

string contentString = "Lorem Ipsum...";
for (int i = 0; i < fileSizeInKB / contentString.Length; i++)
  //write contentString to file

if (fileSizeInKB % contentString.Length > 0)
  // write remaining substring of contentString to file

Edit: If you're saving in Unicode you may need to half the filesize count because unicode uses two bytes per character if I remember correctly.

编辑:如果你保存的是Unicode,那么你可能需要一半的filesize计数,因为如果我没有记错的话,Unicode的每个字符使用两个字节。

#3


0  

There are so many variations on how you can do this. One would be, fill the file with a bunch of chars. You need 100KB? No problem.. 100 * 1024 * 8 = 819200 bits. A single char is 16 bits. 819200 / 16 = 51200. You need to stick 51,200 chars into a file. But consider that a file may have additional header/meta data, so you may need to account for that and decrease the number of chars to write to file.

如何做到这一点有很多不同的方法。一种是,用一堆字符填充文件。你需要100 kb ?没有问题. .100 * 1024 * 8 = 819200位。一个字符是16位。819200 / 16 = 51200。您需要将51200个字符插入到一个文件中。但是考虑到一个文件可能有额外的头/元数据,所以您可能需要考虑到这一点,并减少要写入文件的字符数。

#4


0  

As a partial answer to your question I recently created a portable WPF app that easily creates 'junk' files of almost any size: https://github.com/webmooch/FileCreator

作为对你问题的部分回答,我最近创建了一个便携式WPF应用程序,可以轻松创建几乎任何大小的“垃圾”文件:https://github.com/webmooch/FileCreator