1 概述:也叫高效流,是对4个基本的FileXxx流的增强
2 按数据类型分类
①字节缓冲流BufferedInputStream ..Out
②字符缓冲流BufferedReader ..Writer
3 缓冲流的基本原理:
在创建流对象时会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少IO次数,从而提高读写效率(耗时少)
一 BufferedInputStream
BufferedInputStream extends FileOutputStream extends OutputStream
因此OutputStream 的成员方法 BufferedInputStream 也具有
构造方法:
BufferedInputStream( OutputStream out )
创建一个新的缓冲输出流,将数据写入指定的底层输出流
BufferedInputStream( OutputStream out ,int size)
创建一个新的缓冲输出流,指定缓冲区大小,将数据写入指定的底层输出流
二 BufferedWriter
BufferedWriter extends Writer
特有方法:
void newLine() 写一行 行分隔符(换行)
三 BufferedReader
BufferedReader extends Reader
特有方法:
String readLine() 读一行文字
举例:百度网盘用到了缓冲流
*/
public class Demo5 {
public static void main(String[] args) throws Exception {
//调用方法
//noVip(); //124
//vip(); //43
supVip(); //1
}
//非会员(普通流一次读写一个字节数据)
public static void noVip() throws Exception {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("E:\\IO流之字节流.md");
FileOutputStream fos = new FileOutputStream("E:\\IO流之字节流(复制1).md");
int a;
while ((a = fis.read()) != -1) {
fos.write(a);
}
//释放资源
fis.close();
fos.close();
long end = System.currentTimeMillis();
System.out.println("非会员拷贝文件耗时:" + (end - start));
}
//普通会员(缓冲流一次读写一个字节数据)
public static void vip() throws Exception {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("E:\\IO流之字节流.md");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("E:\\IO流之字节流(复制2).md");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int a;
while ((a = bis.read()) != -1) {
bos.write(a);
}
//释放资源
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println("普通会员拷贝文件耗时:" + (end - start));
}
//超级会员(使用缓存流一次读写一个字节数组)
public static void supVip() throws Exception {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("E:\\IO流之字节流.md");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("E:\\IO流之字节流(复制3).md");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int a;
byte[] bytes = new byte[1024];
while ((a = bis.read(bytes)) != -1) {
bos.write(bytes, 0, a);
}
//释放资源
bis.close();
bos.close();
long end = System.currentTimeMillis();
System.out.println("超级会员拷贝文件耗时:" + (end - start));
}
}
标签:fis,字节,fos,BufferedInputStream,缓冲,System,new From: https://www.cnblogs.com/Sco-/p/16990047.html