简体   繁体   English

如何使用Perl创建虚拟文件?

[英]How to create a dummy file with Perl?

How would I create a dummy file with Perl? 如何使用Perl创建虚拟文件? I need to create a few million files (approx. 5KB - 1MB each), so it is very important to do it as fast as possible. 我需要创建几百万个文件(每个文件大约5KB-1MB),因此尽快完成它非常重要。 So, what would be the fastest way to create a dummy file with Perl? 那么,用Perl创建虚拟文件的最快方法是什么?

my $DUMMY;

open $DUMMY, '>', 'dummy.file' and close $DUMMY;

"OK, this works, but how to define dummy file with exact size, say 2KB ?" “好吧,这行得通,但是如何定义精确大小为2KB的虚拟文件呢?” – user1039509 – user1039509

This should work: 这应该工作:

open FH, '>', 'dummy.file' and print FH "X" x 2048 and close FH or die $!;

If you don't care what the files contain, using seek might be slightly faster yet: 如果您不关心文件包含的内容,使用seek可能会稍快一些:

open FH, '>', 'dummy.file' and seek FH, 2047, 0 and print FH "\0"
    and close FH or die $!;

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

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