![](/img/trans.png)
[英]Fastest way to create a trie (JSON) from a 4GB file, using only 1GB of ram?
[英]Most efficient way to create a large file (> 1GB)
我想知道在java中創建一個非常大的虛擬文件的最有效方法是什么。 文件大小應該高於1GB。 它將用於單元測試只接受文件<= 1GB的方法。
創建稀疏文件。 也就是說,打開一個文件,尋找1GB以上的位置並寫入一些字節。
相關: 使用Java創建給定大小的文件
你不能做一個返回大於1GB的文件大小的模擬? 文件IO對我來說聽起來不是很單位(雖然這取決於你對單元測試的看法)。
使此功能可以創建稀疏文件
private boolean createSparseFile(String filePath, Long fileSize) {
boolean success = true;
String command = "dd if=/dev/zero of=%s bs=1 count=1 seek=%s";
String formmatedCommand = String.format(command, filePath, fileSize);
String s;
Process p;
try {
p = Runtime.getRuntime().exec(formmatedCommand);
p.waitFor();
p.destroy();
} catch (IOException | InterruptedException e) {
fail(e.getLocalizedMessage());
}
return success;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.