与在Linux系统上快速创建大文件相同, 我想在Windows系统上快速创建一个大文件。大的我想是5gb。内容并不重要。内置命令或短批处理文件将是更可取的,但如果没有其他简单的方法,我将接受应用程序。


当前回答

快速执行还是在键盘上快速输入?如果你在Windows上使用Python,你可以尝试这样做:

cmd /k py -3 -c "with open(r'C:\Users\LRiffel\BigFile.bin', 'wb') as file: file.truncate(5 * 1 << 30)"

其他回答

临时文件应该存储在Windows临时文件夹中。根据Rod的回答,您可以使用下面的一行代码创建一个5 GB的临时文件,该文件返回文件名

[System.IO.Path]::GetTempFileName() | % { [System.IO.File]::Create($_).SetLength(5gb).Close;$_ } | ? { $_ }

解释:

[System.IO.Path]::GetTempFileName() generates a random filename with random extension in the Windows Temp Folder The Pipeline is used to pass the name to [System.IO.File]::Create($_) which creates the file The file name is set to the newly created file with .SetLength(5gb). I was a bit surprised to discover, that PowerShell supports Byte Conversion, which is really helpful. The file handle needs to be closed with .close to allow other applications to access it With ;$_ the filename is returned and with | ? { $_ } it is ensured that only the filename is returned and not the empty string returned by [System.IO.File]::Create($_)

你可以试试下面的c++代码:

#include<stdlib.h>
#include<iostream>
#include<conio.h>
#include<fstream>
#using namespace std;

int main()
{
    int a;
    ofstream fcout ("big_file.txt");
    for(;;a += 1999999999){
        do{
            fcout << a;
        }
        while(!a);
    }
}

可能需要一些时间来生成,这取决于你的CPU速度…

Python中的简单回答:如果你需要创建一个大的真正的文本文件,我只是使用了一个简单的while循环,并能够在大约20秒内创建一个5gb的文件。我知道这很粗糙,但已经够快了。

outfile = open("outfile.log", "a+")

def write(outfile):
    outfile.write("hello world hello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello worldhello world"+"\n")
    return

i=0
while i < 1000000:
    write(outfile)
    i += 1
outfile.close()

查看RDFC http://www.bertel.de/software/rdfc/index-en.html

RDFC可能不是最快的,但它确实可以分配数据块。最快的方法必须使用较低级别的API来获取集群链,并将它们放入MFT中,而不写入数据。

注意,这里没有银弹-如果“创建”立即返回,这意味着你得到了一个稀疏文件,它只是一个假的大文件,但你不会得到数据块/链,直到你写入它。如果你只是阅读,你会得到非常快的零,这可能会让你相信你的驱动器突然变得非常快:-)

除了编写一个完整的应用程序,我们Python人可以用四行实现任何大小的文件,在Windows和Linux上使用相同的代码片段(os.stat()行只是一个检查):

>>> f = open('myfile.txt','w')
>>> f.seek(1024-1) # an example, pick any size
>>> f.write('\x00')
>>> f.close()
>>> os.stat('myfile.txt').st_size
1024L
>>>