为什么写一个存在文件比写一个新的空文件更快

时间:2017-01-11 18:37:02

标签: java linux file nio

我使用MappedByteBuffer在linux中编写文件。

File file = new File("testFile");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
MappedByteBuffer mbf = fc.map(FileChannel.MapMode.READ_WRITE, 0, file.length());
mbf.put(buffer);

如果testFile写入500MB,并且写入500MB数据两次,则需要1次。但是当我使用testFile时,写入500MB数据需要4s。

为什么覆盖文件比写新文件更快? 如何以覆盖文件的速度更快地编写新文件?

0 个答案:

没有答案