我编写了一个程序,通过用零填充它来创建任何所需大小的文件。我试图通过每隔几秒(让我们用 5 秒)而不是每个循环刷新它来优化它。当我尝试使用 Timer 时,代码output.flush();
给了我一个错误。
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("");
TimerTask fileProcessTask = new TimerTask()
{
@Override
public void run()
{
output.flush();
processFile();
}
};
Timer tm = new Timer();
tm.schedule(fileProcessTask, 5000L);
size = fp.getFileSize(args[1]);
double avg = (double) size / mem * 100;
System.out.println(avg + "% complete");
}
output.close();
System.out.println("Finished at " + size / 1073741824 + " Gigabyte(s)");
}