博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA中的内存映射文件
阅读量:6967 次
发布时间:2019-06-27

本文共 3298 字,大约阅读时间需要 10 分钟。

hot3.png

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

你可能感兴趣的文章
对象比较:Comparable 和 Comparator
查看>>
jsp中的contentType与pageEncoding的区别和作用
查看>>
java 调用启动远程shell脚本,启动spark
查看>>
Spring boot ----RestTemplate学习笔记
查看>>
[LUOGU] P3128 [USACO15DEC]最大流Max Flow
查看>>
windows2003server下能安装的MSN
查看>>
Caffe将自己的文件生成lmdb
查看>>
C# 枚举中的位运算
查看>>
Codeforces Global Round 1 晕阙记
查看>>
百度文化秘籍
查看>>
Algs4-1.3.33一个双向队列Deque-双向链表实现
查看>>
Algs4-2.2.29自然的归并排序(未解决)
查看>>
shell中数组基础语法
查看>>
P1215 母亲的牛奶
查看>>
回头再看第一次项目
查看>>
有无关键字new的区别
查看>>
Hashmap,Set,Map,List,ArrayList的区别
查看>>
3.Linux 文件的压缩与打包
查看>>
JAVA分布式架构
查看>>
导入自定义模块model
查看>>