1. 程式人生 > >用cmd命令快速建立一個1GB的檔案,然後用java快速讀取

用cmd命令快速建立一個1GB的檔案,然後用java快速讀取

一.建立一個1GB的檔案

使用fsutil命令來建立

1.以管理員身份執行命令提示符

2.開始建立 輸入命令:fsutil file createnew e:\1GB.txt 1073741824(1GB)回車

建立成功,去相應的碟符地下找到檢視屬性就是1GB


二.使用java來快速讀取檔案

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class test1 {
    public static void main(String[] args) throws IOException {
        long begin = System.currentTimeMillis();
        FileInputStream is = new FileInputStream("E:" + File.separator + "1GB.txt");
        byte[] bytes=new byte[64*1024*1024];//64MB
        int length=0;
        while((length=is.read(bytes))!=-1){
            System.out.println(new String(bytes,0,length));
        }
        is.close();
        long end = System.currentTimeMillis();
        System.out.println("用時"+(end-begin));
        //當緩衝陣列的大小是1GB時,會有如下異常
        // Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        //at com.westos.test.Readerfile.main(Readerfile.java:11)
        //java虛擬機器預設的最大記憶體是64MB,所以此異常是超出異常
        //若緩衝記憶體大小為64MB,則用時為35937
    }

注:緩衝池的大小不能超過JVM的預設最大記憶體