Java常用读写类的速度比较

一、本文读写的内容为简单的字符串,主要比较了BufferedWriter(BufferedReader)、BufferedOutputStream(BufferedInputStream)、ByteBuffer(write&read)和MappedByteBuffer(write&read)的读写速度差异。测试时都是先写入文件5000000个字符串“Java is one of the best computer languages!\n”,然后再将所有数据读取到内存中。程序分别记录了上述四种方法的读写速度。


测试程序为:

import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.io.OutputStreamWriter;  
import java.io.RandomAccessFile;  
import java.nio.ByteBuffer;  
import java.nio.channels.FileChannel;  

public class IOStudy {  

    public static void main(String[] args) {  
        test1();//BufferedWriter(BufferedReader)  
        test2();//BufferedOutputStream(BufferedInputStream)  
        test3();//ByteBuffer(write&read)  
        test4();//MappedByteBuffer(write&read)  
    }  

    public static void test1() {  
        BufferedWriter bw = null;  
        BufferedReader br = null;  
        long time;  
        try {  
            time = System.currentTimeMillis();  
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(".//data//test1.txt"), "utf-8"));  
            for(int i = 0; i < 5000000; i++) {  
                bw.write("Java is one of the best computer languages!\n");  
            }  
            bw.flush();  
            System.out.println("BufferedWriter: " + (System.currentTimeMillis() - time) + "ms");  

            time = System.currentTimeMillis();  
            br = new BufferedReader(new InputStreamReader(new FileInputStream(".//data//test1.txt"), "utf-8"));  

            //要慢一点605ms,但是使用比较方便  
//          String inputline = br.readLine();  
//          while(inputline != null) {  
//              inputline = br.readLine();  
//          }  
            //要快一点  
            char[] data = new char[5000000 * 44];  
            br.read(data);  
            System.out.println("BufferedReader: " + (System.currentTimeMillis() - time) + "ms");  

        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if(bw != null)  bw.close();  
                if(br != null)  br.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

    public static void test2() {  
        BufferedOutputStream bos = null;  
        BufferedInputStream bis = null;  
        long time;  
        try {  
            time = System.currentTimeMillis();  
            bos = new BufferedOutputStream(new FileOutputStream(".//data//test2.txt"));  
            for(int i = 0; i < 5000000; i++) {  
                bos.write("Java is one of the best computer languages!\n".getBytes());  
            }  
            bos.flush();  
            System.out.println("BufferedOutputStream: " + (System.currentTimeMillis() - time) + "ms");  

            time = System.currentTimeMillis();  
            bis = new BufferedInputStream(new FileInputStream(".//data//test2.txt"));  
            byte[] data = new byte[5000000 * 44];  
            bis.read(data);  
            System.out.println("BufferedInputStream: " + (System.currentTimeMillis() - time) + "ms");  

        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if(bos != null) bos.close();  
                if(bis != null) bis.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

    public static void test3() {  
        FileOutputStream fos = null;  
        FileChannel fc1 = null;  
        FileInputStream fis = null;  
        FileChannel fc2 = null;  
        long time;  
        try {  
            time = System.currentTimeMillis();  
            fos = new FileOutputStream(".//data//test3.txt");  
            fc1 = fos.getChannel();  
            ByteBuffer b = ByteBuffer.wrap("Java is one of the best computer languages!\n".getBytes());  
            for(int i = 0; i < 5000000; i++) {  
                fc1.write(b);  
                b.rewind();  
            }  
            System.out.println("ByteBuffer(write): " + (System.currentTimeMillis() - time) + "ms");  

            time = System.currentTimeMillis();  
            fis = new FileInputStream(".//data//test3.txt");  
            fc2 = fis.getChannel();  
            ByteBuffer data = ByteBuffer.allocate(5000000 * 44);  
            fc2.read(data);  
            System.out.println("ByteBuffer(read): " + (System.currentTimeMillis() - time) + "ms");  

        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if(fc1 != null) fc1.close();  
                if(fc2 != null) fc1.close();  
                if(fos != null) fos.close();  
                if(fis != null) fis.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

    public static void test4() {  
        FileChannel fc1 = null;  
        FileChannel fc2 = null;  
        long time;  
        try {  
            time = System.currentTimeMillis();  
            fc1 = new RandomAccessFile(".//data//test4.txt", "rw").getChannel();  
            ByteBuffer b = fc1.map(FileChannel.MapMode.READ_WRITE, 0, 5000000 * 44);  
            for(int i = 0; i < 5000000; i++) {  
                b.put("Java is one of the best computer languages!\n".getBytes());  
            }  
            System.out.println("MappedByteBuffer(write): " + (System.currentTimeMillis() - time) + "ms");  

            time = System.currentTimeMillis();  
            fc2 = new FileInputStream(".//data//test4.txt").getChannel();  
            b = fc2.map(FileChannel.MapMode.READ_ONLY, 0, fc2.size());  
            byte[] data = new byte[5000000 * 44];  
            b.get(data);  
            System.out.println("MappedByteBuffer(read): " + (System.currentTimeMillis() - time) + "ms");  

        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if(fc1 != null) fc1.close();  
                if(fc2 != null) fc2.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
} 

测试结果为:

可以看出来,BufferedWriter的写速度是最快的,MappedByteBuffer的读速度是最快的,但是一般BufferedReader操作方便,并且其速度也不是特别慢。所以一般情况下建议字符串读写用BufferedWriter&BufferedReader;对于全文读速度要求高的话可以用MappedByteBuffer。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值