我目前有一个工作程序,它通过用零填充它来创建任何所需大小的文件。这很好,但我需要执行此操作的文件大小为千兆字节,并且此方法将花费很长时间才能执行此操作。如果有人可以阅读此代码并提供提示以使其更快,将不胜感激。
public class fileMaker
{
public static void main(String[] args) throws IOException
{
fileMaker fp = new fileMaker();
Writer output = null;
File f = new File(args [1]);
output = new BufferedWriter(new FileWriter(f, true));
output.write("0");
long size = fp.getFileSize(args[1]);
long mem = Long.parseLong(args[0]) * 1073741824; //1 Gigabyte = 1073741824 bytes
while(size < mem)
{
output.write("0");
output.flush();
size = fp.getFileSize(args[1]);
//System.out.println(size + " bytes completed out of " + mem);
double avg = (double)size / mem * 100;
System.out.println(avg + "% complete");
}
output.close();
System.out.println("Finished at - " + size / 1073741824 + " Gigabytes");
}
private long getFileSize(String fileName)
{
File file = new File(fileName);
if (!file.exists() || !file.isFile())
{
System.out.println("File does not exist");
return -1;
}
return file.length();
}
}