缓冲流:

  读取数据大量的文件时,读取的速度慢,java提供了一套缓冲流,提高IO流的效率;

  缓冲流分为字节缓冲流和字符缓冲流;

  字节输入缓冲流和字节输出缓冲流如下:

package com.zs.Demo;

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

public class Demo {
public static void main(String[] args) {
try {
fun();
fun1();
} catch (IOException e) {
e.printStackTrace();
}
}
//字节缓冲输入流
private static void fun1() throws IOException {
//第二种创建缓冲流方式
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("d:\\a.txt"));
int len=0;
while((len=bis.read())!=-1){
System.out.println((char)len);
}
bis.close();
}
//字节缓冲输出流
private static void fun() throws IOException {
//第一种方式
FileOutputStream f=new FileOutputStream("d:\\a.txt");
BufferedOutputStream bos=new BufferedOutputStream(f);
//写入一个字节
bos.write(105);
bos.write("hello world".getBytes());//写入字节数组
bos.write("hello world".getBytes(),0,2);//写入字节数组指定内容
bos.close();
}
}


  字符输入缓冲流和输出缓冲流如下:

package com.zs.Demo;

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

public class Demo2 {
public static void main(String[] args) {
try {
fun();
fun1();
} catch (IOException e) {
e.printStackTrace();
}
}
//字符输出缓冲流
private static void fun() throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\b.txt"));
bw.write(111);//自动查码表,编码
bw.flush();//注意字符输出流每次操作都要刷新
bw.write("hello world".toCharArray());//写入字符数组
bw.flush();
bw.newLine();//newLine()特有方法,换行
bw.flush();
bw.write("java");//写入字符串
bw.flush();
bw.close();
}
//字符输入缓冲流
private static void fun1() throws IOException {
//字符输入缓冲流
BufferedReader br=new BufferedReader(new FileReader("d:\\b.txt"));
int len=0;
while ((len=br.read())!=-1) {
System.out.print((char)len);
}
br.close();

//创建字符数组输入缓冲流对象
BufferedReader br1=new BufferedReader(new FileReader("d:\\b.txt"));
int len1=0;
char[] c=new char[1024];
while((len1=br1.read(c))!=-1){
System.out.print(new String(c,0,len1));
}
br1.close();

//字符输入缓冲流特有的方法readLine() 一次读取一行
BufferedReader br2=new BufferedReader(new FileReader("d:\\b.txt"));
String len3=null;//这里注意是字符串类型
while((len3=br2.readLine())!=null){
System.out.println(len3);
}
br2.close();
}
}


下面写一个比较字节流,字节数组流,字节缓冲流,字节数组缓冲流复制文件速度的代码:

package com.zs.Demo;

import java.io.*;

public class Demo5 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
try {
//这里一个一个方法的运行,比较时间,复制的文件是一个240M的视频文件
// fun1("h:\\1.mp4","g:\\1.mp4");//字节流
// fun2("h:\\1.mp4","g:\\1.mp4");//字节数组流
// fun3("h:\\1.mp4","g:\\1.mp4");//字节缓冲流
fun4("h:\\1.mp4","g:\\1.mp4");//字节数组缓冲流
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end-start);
}
//字节数组缓冲流
private static void fun4(String s, String s1) throws IOException {
BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));
int len=0;
byte[] b=new byte[1024*10];
while((len=fi.read(b))!=-1){
fo.write(b,0,len);
}//1653毫秒
fo.close();
fi.close();
}
//字节缓冲流
private static void fun3(String s, String s1) throws IOException {
BufferedInputStream fi=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream(s1));
int len=0;
while((len=fi.read())!=-1){
fo.write(len);
}//13015毫秒
fo.close();
fi.close();
}
//字节数组流
private static void fun2(String s, String s1) throws IOException {
FileInputStream fi=new FileInputStream(new File(s));
FileOutputStream fo=new FileOutputStream(new File(s1));
byte[] b=new byte[1024*10];
int len=0;
while((len=fi.read(b))!=-1){
fo.write(b,0,len);
}//6979毫秒
fo.close();
fi.close();
}
//字节流
private static void fun1(String s, String s1) throws IOException {
FileInputStream fi=new FileInputStream(new File(s));
FileOutputStream fo=new FileOutputStream(new File(s1));
int len=0;
while((len=fi.read())!=-1){
fo.write(len);
}//太慢了,等不下去了,时间十分钟以上
fo.close();
fi.close();
}
}


可以看出字节数组缓冲流速度最快,