简体   繁体   English

生成给定大小的文件

[英]Generate file of given size

For test purposes I'd like to get file of given size filled with random data. 出于测试目的,我想获取给定大小的文件,其中填充了随机数据。

So I have two questions: 所以我有两个问题:

  • How to efficiently generate file of given size in Android? 如何在Android中有效生成给定大小的文件?
  • How to efficiently fill the file with random data ? 如何有效地用随机数据填充文件?
    • What is the best source (ideally compatible back to v7) of random bytes? 随机字节的最佳来源(理想情况是兼容v7)是什么?
    • Is it necessary, because the file would contain data previously written in allocated harddisk space? 是否有必要,因为文件将包含先前写入分配的硬盘空间中的数据?

You just create the file and fill it with the size you want. 您只需创建文件并用所需的大小填充它。 You don't 'set the size'. 您无需“设置大小”。 If you create a file and put 1 kilobytes of stuff into it the file will be 1 kilobyte (*slightly larger actually). 如果创建文件并将1 KB的内容放入其中,则该文件将为1 KB(实际上*稍大)。

So just create a file, and use whatever file write routine to output bytes which you can create with (byte)(random()*510 - 255) in a loop for however many bytes large you want the file to be. 因此,只需创建一个文件,然后使用任何文件写入例程来输出您可以使用(byte)(random()*510 - 255)创建的(byte)(random()*510 - 255)无论文件多大字节数(byte)(random()*510 - 255) ,都可以循环输出。 Since this is java and you may want to use print to output to the file, you have to use (char)(Math.random()*510 - 255) otherwise it will print the string "65" instead of "A" 由于这是Java,并且您可能要使用print来输出到文件,因此必须使用(char)(Math.random()*510 - 255)否则它将打印字符串“ 65”而不是“ A”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM