我目前有一个工作程序,通过用零填充来创建任何所需大小的文件。这很棒,但我需要执行此操作的文件的大小是千兆字节,并且此方法将永远需要执行此操作。如果任何人都可以阅读此代码,并提供提高速度的提示,将不胜感激。
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();
}
}
答案 0 :(得分:3)
size
。