IO进阶------------------------------字节缓冲流

概念


之前也可以实现读写,复制等功能

但是读写最让人头疼的是效率

想想纯拿int循环写入的效率 

之后拿byte[]数组复制文件的效率 

完全不是一个级别的

那么java 觉得光拿 byte [] 数组还是不够效率,所以出现了缓冲流


缓冲类的类名都是以Buffered开头的

类如

扫描二维码关注公众号,回复: 1794246 查看本文章

BufferedInputStream    字节流写入缓冲类

BufferedOutputStream      字节流写出缓冲类

BufferedReader        字符流写入缓冲类

BufferedWriter         字符流写出缓冲类

自己再重申一遍   input 是往程序里写,output 是往文件里写

缓冲流小示例

public class MainTest2 {

	public static void main(String []args) throws IOException{
		
		long start = System.currentTimeMillis();
		
		FileInputStream is = null;
		FileOutputStream os = null;
		BufferedOutputStream bs = null;
		BufferedInputStream bi = null;
		
		try {
			is = new FileInputStream("E:\\io\\nox_setup_v6.1.0.0_full.exe");
			
			os = new FileOutputStream("E:\\iocopy\\nox_setup_v6.1.0.0_full.exe");
			//buffered缓冲流对象就是要把 字节流对象传入就可以了 他来操作 和之前调用的方式一摸一样
			bi = new BufferedInputStream(is);
			
			bs = new BufferedOutputStream(os);
			
			byte[] b = new byte[1024*1024];
			
			int len =  0;
			
			while ((len = bi.read(b)) != -1) {
				
				bs.write(b, 0, len);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bs.close();
			os.close();
			is.close();
		}
		long end = System.currentTimeMillis();
		
		System.out.println("执行时间:"+(end-start)+"ms");
	}
	

但是吧,这个示例完全没有展示出缓冲流的加速效果。。。。。。。。。 还比直接用流慢了一丢丢,也许是我写错了待考证

嗯 , 又从新测试了一下  是快了一点  以后这个就是暂时的标准io流格式

还有字符缓冲流 暂时先不研究



猜你喜欢

转载自blog.csdn.net/jiulanhao/article/details/80840527