java输入输出专题--第一部分

主要对普通io的读写效率进行对比。

代码如下:

public class IOTest {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        
        outputStreamTest();
        // bufferedOutputStreamTest();
        randomAccessFileWriteTest();
        // inputStreamTest();
        // bufferedInputStreamTest();
        // randomAccessFileReadTest();
    }

    private static void outputStreamTest() throws Exception {
        FileOutputStream out = new FileOutputStream(new File("data"));
        byte[] value = new byte[1024];
        int lop = 1024 * 1024;
        long start = System.currentTimeMillis();
        for (int i = 0; i < lop; i++) {
            out.write(value);
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start);
        out.close();

    }

    private static void bufferedOutputStreamTest() throws Exception {
        FileOutputStream out = new FileOutputStream(new File("data"));
        BufferedOutputStream bOut = new BufferedOutputStream(out);
        byte[] value = new byte[1024];
        int lop = 1024 * 1024;
        long start = System.currentTimeMillis();
        for (int i = 0; i < lop; i++) {
            bOut.write(value);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        bOut.close();

    }

    private static void randomAccessFileWriteTest() throws Exception {
        RandomAccessFile raf = new RandomAccessFile(new File("data"), "rw");
        byte[] value = new byte[1024];
        int lop = 1024 * 1024;
        long start = System.currentTimeMillis();
        for (int i = 0; i < lop; i++) {
            raf.write(value);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
        raf.close();
    }

    private static void inputStreamTest() throws Exception {
        FileInputStream in = new FileInputStream(new File("data"));
        byte[] buf = new byte[1024];
        int readCount = -1;
        long totalCount = 0;
        long start = System.currentTimeMillis();
        while ((readCount = in.read(buf)) != -1) {
            totalCount += readCount;
        }
        long end = System.currentTimeMillis();
        System.out.println("读取:" + totalCount + "个字节,耗时:" + (end - start));
        in.close();
    }

    private static void bufferedInputStreamTest() throws Exception {
        FileInputStream in = new FileInputStream(new File("data"));
        BufferedInputStream bin = new BufferedInputStream(in);
        byte[] buf = new byte[1024];
        int readCount = -1;
        long totalCount = 0;
        long start = System.currentTimeMillis();
        while ((readCount = bin.read(buf)) != -1) {
            totalCount += readCount;
        }
        long end = System.currentTimeMillis();
        System.out.println("读取:" + totalCount + "个字节,耗时:" + (end - start));
        bin.close();
    }

    private static void randomAccessFileReadTest() throws Exception {
        RandomAccessFile raf = new RandomAccessFile(new File("data"), "r");
        byte[] buf = new byte[1024];
        int readCount = -1;
        long totalCount = 0;
        long start = System.currentTimeMillis();
        while ((readCount = raf.read(buf)) != -1) {
            totalCount += readCount;
        }
        long end = System.currentTimeMillis();
        System.out.println("读取:" + totalCount + "个字节,耗时:" + (end - start));
        raf.close();
    }

}

测试结果如下:

写效率:BufferedOutputStream > FileOutputStream  >  RandomAccessFile

读效率:BufferedInputStream  >  FileInputStream  约等于 RandomAccessFile

 

转载于:https://my.oschina.net/u/1268334/blog/2245787

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值