BufferedInputStream和BufferedOutputStream类(对In/out—putStream达到性能优化)(简单代码例子实现)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class Main{  
	 public static void  main(String[] args) {
		File f = new File("word.txt");
		//缓冲输出流的作用:提高了运行效率
		FileOutputStream out = null ; 
		BufferedOutputStream bo =null;
		 try {
			out = new FileOutputStream(f);
			bo = new BufferedOutputStream(out);//包装文件输出流
			String s = "你好baby";
			byte b[] = s.getBytes();
//			out.write(b);
			bo.write(b);
			//使用缓冲字节输出流时,更多的进行刷新操作
			bo.flush();//刷新。强制将缓冲区取的数据写入文件中,即使缓冲区没有写满
			
			out.close();
		} catch (FileNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		

		//缓冲输入流的作用:大大的提高了运行效率
//			File f = new File("D:\\JAVA中文介绍\\JAVA中文介绍.CHM");
//		long start = System.currentTimeMillis();//获取流开始时的毫秒值	
//		FileInputStream in = null;
//		BufferedInputStream bi = null;//缓冲字节流的效果:大大的提高了运行效率
//		
//		try {
//			in = new FileInputStream(f);
//			bi = new BufferedInputStream(in);//将文件字节流包装成缓冲流
//			byte by[] = new byte[1024];//缓冲区字节数组(这个缓冲区与Buffered不同)
//			while(in.read(by) != -1) {//使用字节流读取数据
//				
//			}
			while(bi.read(by) != -1) {//使用缓冲流读取数据
//				
			}
//			long end = System.currentTimeMillis();//获取流结束时毫秒值
//			System.out.println("运行的时间 : " + (end-start));
//			
//		} catch (FileNotFoundException e) {
//			// TODO 自动生成的 catch 块
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO 自动生成的 catch 块
//			e.printStackTrace();
//		} finally {
//			if(in != null) {
//				try {
//					in.close();
//				} catch (IOException e) {
//					// TODO 自动生成的 catch 块
//					e.printStackTrace();
//				}
//			}
//		}

	 }
}

第二部分的结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值