日期:2014-05-16  浏览次数:21022 次

使用RandomAccessFile产生固定大小文件

产生特定大小的文件,在测试某些test case的时候会非常有用,可以使用RandomAccessFile类来实现:

package io;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class BigFileTester {

	public static void main(String[] args) throws FileNotFoundException, IOException {
		int cap = 3*1024*1024;
		
		long start2 = System.currentTimeMillis();
		RandomAccessFile r = new RandomAccessFile("C:\\test2.txt", "rw");
		r.setLength(cap);
		r.close();
		long duration2 = System.currentTimeMillis() - start2;
		System.out.println(duration2);		
	}

}

?