版权声明: https://blog.csdn.net/weixin_40072979/article/details/83626057
缓冲流是对字节流的缓冲,可以加快字节流的效率。使用缓冲流的前提是必须使用字节流,我们只需要关闭最外面的流就行了,内部的留会自动关闭。
1:缓冲字节输入流
package com.jianshun;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 四个步骤: 分段读取 文件字节输入流 加入缓冲流
* 1、创建源
* 2、选择流
* 3、操作
* 4、释放资源
*/
public class BufferedTest01 {
public static void main(String[] args) {
//1,创建流
File src = new File("abc.txt");
//2,选择流
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(src));
//3,操作(分段读取)
byte[] flush = new byte[1024];//缓冲容器
int len = -1;
while((len = in.read(flush))!=-1){
//字节数组--》字符串 (解码)
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4,释放资源
if(null!=in){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2:缓冲字节输出流
package com.jianshun;
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;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 四个步骤: 分段读取 文件字节输入流 加入缓冲流
* 1、创建源
* 2、选择流
* 3、操作
* 4、释放资源
*/
public class BufferedTest02 {
public static void main(String[] args) {
File src = new File("dest.txt");
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(src));
String msg = "IO is so easy\r\n";
byte[] datas = msg.getBytes();
out.write(datas, 0, datas.length);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(null!=out){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我们可以改进之前复制文件的方法,用字节缓冲流复制文件
package com.jianshun;
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;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 缓冲字节流拷贝文件
* @author Administrator
*/
public class Copy02 {
public static void main(String[] args) {
copy("abc.txt","dest.txt");
}
public static void copy(String srcPath,String destPath){
//,2,创建源
File src = new File(srcPath);
File dest = new File(destPath);
//,2,选择流
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src));
out =new BufferedOutputStream(new FileOutputStream(dest));
//3,操作,分段读取
byte[] flush = new byte[1024];
int len = -1;//接收长度
while((len = in.read(flush)) != -1){
out.write(flush, 0, len);//分段写出
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//4,释放资源,分别关闭,先打开的后关闭
if(out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in !=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void copy2(String srcPath,String destPath){
File src = new File(srcPath);//源头
File dest = new File(destPath);
try(InputStream in = new BufferedInputStream(new FileInputStream(srcPath));
OutputStream out = new BufferedOutputStream(new FileOutputStream(destPath))) {
//操作,分段读取
byte[] flush = new byte[1024];
int len = -1;
while((in.read(flush))!=-1){
out.write(flush, 0, len);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}