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


当前回答

你可以试试下面的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()

我一直在寻找一种方法来生成包含数据的大文件,而不仅仅是稀疏文件。遇到下面的技巧:

如果您想要创建一个包含真实数据的文件,那么您可以使用下面的命令行脚本。 这只是一个示例行追加创建一个大文件.." > dummy.txt /L %i in (1,1,14) do type dummy.txt >> dummy.txt (依次运行以上两个命令,或者可以将它们添加到批处理文件中。) 上面的命令在几秒钟内创建一个1mb的文件dummy.txt…

临时文件应该存储在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($_)

我在https://github.com/acch/genfiles上找到了一个可配置的优秀实用程序。

它用随机数据填充目标文件,因此使用稀疏文件没有问题,而且对于我的目的(测试压缩算法)来说,它提供了不错的白噪声水平。

打开Windows任务管理器,右键单击正在运行的最大进程,然后单击“创建转储文件”。

这将在临时文件夹中创建一个相对于内存中进程大小的文件。

您可以轻松地创建一个千兆字节大小的文件。