JavaIO流读写数据速度比较

  • 最近线上经常出现多文件下载,JVM内存爆了的情况,借此复习一下IO读写数据,文章就常规IO流读写数据、缓冲IO流读写数据、NIO读写数据做测试。
  • 结果:
    普通IO流效果最差;
    缓冲流使用默认size即8192 和 NIO 8192Buffer的速度接近;修改缓冲流size为16384 、 NIO Buffer为16384,速度仍然接近;但是NIO相对来说占有优势。(耗时单位为mills,值为20次结果取均值,读取文件大小102MB)
    在这里插入图片描述
  • 代码
class Test{
	public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\vedio.ev4");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\testA\\vedio.ev4");

        long beginMethodA = System.currentTimeMillis();
        methodA(); //IO流
        long endMethodA = System.currentTimeMillis();
        System.out.println( "methodA count " + (endMethodA-beginMethodA));


        long beginMethodB = System.currentTimeMillis();
        methodB();//缓冲区IO流
        long endMethodB = System.currentTimeMillis();
        System.out.println( "methodB count " + (endMethodB-beginMethodB));

        long beginMethodC = System.currentTimeMillis();
        methodC(); //NIO
        long endMethodC = System.currentTimeMillis();
        System.out.println( "methodB count " + (endMethodC-beginMethodC));

    }



    public static void methodA() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\vedio.ev4");
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\testA\\vedio.ev4");
        byte[] bytes = new byte[1024];
        int len;
        try {
            while( (len  = fileInputStream.read(bytes)) != -1 ){
                fileOutputStream.write(bytes,0,len);
            }
        }catch (Exception e){
            System.out.println("exception");
        }finally {
            if(fileInputStream != null){
                fileInputStream.close();
            }
            if(fileOutputStream != null){
                fileOutputStream.close();
            }
        }
    }


    public static void methodB() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\vedio.ev4");
        BufferedInputStream bis = new BufferedInputStream(fileInputStream);
        //BufferedInputStream bis = new BufferedInputStream(fileInputStream,16384);
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\testA\\vedio2.ev4");
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        //BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream ,16384);
        byte[] bytes = new byte[1024];
        int len;
        try {
            while( (len  = bis.read(bytes)) != -1 ){
                bos.write(bytes,0,len);
            }
        }catch (Exception e){
            System.out.println("exception");
        }finally {
            if(bis != null){
                bis.close();
            }
            if(bos != null){
                bos.close();
            }
        }
    }

    /**
     * 核心对象是selector
     * */
    public static void methodC() throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\vedio.ev4","rw");
        RandomAccessFile randomAccessFile1 = new RandomAccessFile("D:\\testA\\vedio3.ev4","rw");
        FileChannel readChannel = randomAccessFile.getChannel();
        FileChannel writeChannel = randomAccessFile1.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(8192);
        //ByteBuffer byteBuffer = ByteBuffer.allocate(16384);
        try{
         int byteRead= readChannel.read(byteBuffer); //返回的是读取的字节数
        while (byteRead!=-1){
            byteBuffer.flip(); //反转读写模式
            while (byteBuffer.hasRemaining()){
                writeChannel.write(byteBuffer);
            }
            byteBuffer.compact();
            byteRead= readChannel.read(byteBuffer);
        }
		}catch(Exception  e ){
			e.printStackTrace();
		}finally{
			if(readChannel != null){
				readChannel.close();
			}
			if( writeChannel != null){
				writeChannel.close();
			}
        	randomAccessFile.close();
        	randomAccessFile1.close();
		}
    }
}


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值