IO流之缓冲流

1.1 缓冲流有什么作用

使用缓冲数组以后,整体的读取,写入效率提升很大,降低了CPU通过内存访问硬盘的次数,提高效率,降低磁盘损耗

字节输入缓冲 BufferedInputStream
字节输出缓冲 BufferedOutputStream
字符输入缓冲 BufferedReader
字符输出缓冲 BufferedWriter

【重点】

所有的缓冲流都没有任何的读取写入文件的能力,都需要对应的输入流和输出流来提供对应的能力
在创建缓冲流对象时,需要传入对应的输入流对象和输出流对象
底层就是提供了一个默认大小的缓冲数组用于提高效率

1.2 字节缓冲流

输入缓冲流:
BufferedInputStream(InputStream in)这里需要的对象是一个字节输入流基类对象,同时也可以传入InputStream子类对象
输出缓冲流
BufferedOutputStream(OutputStream out) 这里需要的对象是一个字节输出流基类对象,同时也可以传入OutputStream子类对象

1.2.1 字节输入流缓冲效率问题

1.在BufferedInputStream底层中有一个默认容量为8KB的Byte类型缓冲数组

2.fill方法是一个操作核心

a.从硬盘中读取数据,读取的数据容量和缓冲数组容量是一致的
b.所有的read方法都是从缓冲数组中读取数据
c.每一次读取数据之前,都会检查缓冲区内是否有数据,如果没有,fill方法执行,填充数据。

3.利用缓冲fill方法可以极大的降低CPU通过内存访问磁盘的次数。同时程序操作的数据是在内存中进行交互的

1.2.2 字节输出流缓冲效率问题

1.在BufferedOutputStream类对象,默认有一个8KB的byte类型缓冲数组
2.数据写入文件时并不是直接保存到文件中,而是保存在内存8KB字节缓冲数组
3.如果8KB空间填满,会直接flush缓冲区,数据直接保存到硬盘中,同时清空整个缓冲区
4.在BufferedOutputStream关闭时,首先会调用flush方法,保存数据到文件,清空缓冲区,并且规划缓冲区占用内存,同时关闭缓冲流使用的字节输出流

使用字节缓冲流代码演示

package bkDay15.buffer;

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

/**
 * 字节缓冲流 BufferedInputStream 和 BUfferedOutputStream
 * 
 * @author xiao.shuo.
 *
 */
public class Demo3_buffer {
	public static void main(String[] args) {
		BufferTest();
	}
	public static void BufferTest() {
		//1.找到对应的文件夹
		File file = new File("/Users/xiao.shuo./Desktop/a.txt");
		FileInputStream fis = null;
		try {
			//2.创建对应的FileInptStream输入字节流对象
			fis = new FileInputStream(file);
			//3.根据文件操作输入字节流对象,创建对应的缓冲流对象
			BufferedInputStream bis = new BufferedInputStream(fis);
			//4.读取数据过程,读取过程中使用的方法是FileInputStream提供的方法
			int content = -1;
			while((content = bis.read()) != -1) {
				System.out.println((char)  content);
			}
			bis.close();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
1.2.3 缓冲流拷贝和非缓冲流拷贝的时间效率区别

代码演示

package bkDay15.buffer;

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

/*
 * 使用字节缓冲流实现文件的拷贝
 */
public class Demo3_bufffer_copy {
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		bufferCopy();
		long end = System.currentTimeMillis();
		System.out.println("时间:" + (end - start));
		
		long start1 = System.currentTimeMillis();
		Copy();
		long end1 = System.currentTimeMillis();
		System.out.println("时间:" + (end1 - start1));
	}
	
	/**
	 * 不使用字节缓冲流
	 */
	public static void Copy() {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("/Users/xiao.shuo./Desktop/b.txt");
			fos = new FileOutputStream("/Users/xiao.shuo./Desktop/d.txt");
			int content = -1;
			while((content = fis.read()) != -1) {
				fos.write(content);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 使用字节缓冲流buffer
	 */
	public static void bufferCopy() {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			bis = new BufferedInputStream(new FileInputStream("/Users/xiao.shuo./Desktop/b.txt"));
			bos = new BufferedOutputStream(new FileOutputStream("/Users/xiao.shuo./Desktop/c.txt"));
		
			int length = -1;
			while((length = bis.read()) != -1) {
				bos.write(length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

1.3 字符缓冲流

BufferedReader 字符缓冲输入流
BufferedWriter 字符缓冲输出流

1.3.1 字符缓冲流效率问题

1.字符缓冲输入流底层有一个8192个元素的缓冲字符数组,而且使用fill方法从硬盘中读取数据填充缓冲数组
2.字符缓冲输出流底层有一个8192个元素的缓冲字符数组,使用flush方法将缓冲数组中的内容写入到硬盘中
3.使用缓冲数组之后,程序在运行的大部分时间内都是内存和内存之间的数据交互过程。
4.关闭字符缓冲流都是首先释放对应的缓冲数组空间,并且关闭创建对应的字符输入流和字符输出流

package bkDay15.buffer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * 字符缓冲流 BufferedReader 和 BufferedWriter
 */
public class Demo4_buffer_char {
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		bufferedReader();
		long end = System.currentTimeMillis();
		System.out.println("时间:" + (end - start));
		
		bufferedWriter();
	}
	
	public static void bufferedWriter() {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter("/Users/xiao.shuo./Desktop/e.txt"));
			bw.write("武汉加油");
			bw.newLine();
			bw.write("中国加油");
			bw.newLine();
			bw.write("冬眠");
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void bufferedReader() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("/Users/xiao.shuo./Desktop/d.txt"));
			System.out.println(br.readLine());
			System.out.println(br.readLine());
			System.out.println(br.readLine());
//			System.out.println("----------");
//			while(br.readLine() != null) {
//				System.out.println(br.readLine());
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(br != null) {
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值