JAVA中的内存映射文件

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.MappedByteBuffer;

import java.nio.channels.FileChannel;

 

public class NIOTest {

    private static int times = 2000000;

    private static String data = "#2015-5-20 14:38:00 revoke() method is invoked...\r\n";;

    private static byte[] bytes = data.getBytes();

 

    public static void ensureNewFileExist(String filename) {

        File file = new File(filename);

        file.deleteOnExit();

        try {

            file.createNewFile();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            System.out.println(e.toString());

        }

    }

 

    public static void fileOutputStream(String filename) throws IOException {

 

        // use fileoutputstream

        FileOutputStream fos = new FileOutputStream(filename);

        for (int i = 0; i < times; i++) {

            fos.write(bytes);

        }

        fos.flush();

        fos.close();

    }

 

    public static void bufferedOutputStream(String filename) throws IOException {

 

        // use BufferedOutputStream

        FileOutputStream fos = new FileOutputStream(filename);

        BufferedOutputStream bos = new BufferedOutputStream(fos);

        for (int i = 0; i < times; i++) {

            bos.write(bytes);

        }

        bos.flush();

        bos.close();

        fos.flush();

        fos.close();

    }

 

    public static void randomAccessFile(String filename) throws IOException {

 

        // use random access file

        RandomAccessFile file = new RandomAccessFile(filename, "rw");

        for (int i = 0; i < times; i++) {

            file.write(bytes);

        }

        file.close();

    }

 

    public static void mappedFile(String filename) throws IOException {

 

        // use mapped file

        // FileOutputStream fos = new FileOutputStream(filename);

        RandomAccessFile out = new RandomAccessFile(filename, "rw");

        FileChannel channel = out.getChannel();

        long size = 200 * 1024 * 1024;

        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, size);

        for (int i = 0; i < times; i++) {

            buffer.put(bytes);

        }

        channel.close();

        out.close();

 

    }

 

    public static void main(String[] args) throws IOException {

        // 1 File Output Stream

        System.out.println("File Output Stream");

 

        String filename = "./1_file_Output_Stream.txt";

        ensureNewFileExist(filename);

        long start = System.currentTimeMillis();

        fileOutputStream(filename);

        long end = System.currentTimeMillis();

        System.out.println((end - start) + " milliseconds\n");

 

        // 2 Buffered Output Stream

        System.out.println("Buffered File Output Stream");

        filename = "./2_buffered_File_Output_Stream.txt";

        ensureNewFileExist(filename);

        start = System.currentTimeMillis();

        bufferedOutputStream(filename);

        end = System.currentTimeMillis();

        System.out.println((end - start) + " milliseconds\n");

 

        // 3 Random Access File

        System.out.println("Random Access Output Stream");

 

        filename = "./3_random_access_Output_Stream.txt";

        ensureNewFileExist(filename);

        start = System.currentTimeMillis();

        randomAccessFile(filename);

        end = System.currentTimeMillis();

        System.out.println((end - start) + " milliseconds\n");

 

        // 4 Mapped File

        System.out.println("Mapped Output Stream");

 

        filename = "./4_mapped_Output_Stream.txt";

        ensureNewFileExist(filename);

        start = System.currentTimeMillis();

        mappedFile(filename);

        end = System.currentTimeMillis();

        System.out.println((end - start) + " milliseconds");

    }

}

------------------------------------------测试结果如下:

153537_3KRY_1382024.png

看来还是比较快的。

转载于:https://my.oschina.net/qiangzigege/blog/417280

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值