java Io 缓冲流 BufferedInputStream BufferedOutputStream 笔记

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 org.junit.Test;


/**
 * 
 * Io流 
 * 
 * 4个抽象基类
 * 
 * 抽象基类                                                       节点流:-- 文件字节流缓冲流  ---处理流:  的一种  (缓冲流:文件操作的效率高。) 
 * InputStream          FileInputStream BufferedInputStream  
 * OutputStream FileOutputStreamBufferedOutputStream   需要--循环后flush();
 * 节点流:-- 文件字符流
 * Reader FileReaderBufferedReader          
 * Writer FileWriterBufferedWriter          需要--循环后flush();
 *   
 * 
 * 
 * 处理流 : ---- 缓冲流  
 * 
 * 
 * BufferedInputStream
 * BufferedOutputStream
 * 
 * BufferedReader-----------------可以继续 用 char[]固定 数组长度     接收 ,   
 *      也可以用 BufferedReader.readLine() 一行行接收。 该方法返回 读取此行的字符串
 * BufferedWriter ----------------- 需要--循环后flush();
 * 
 * 缓冲流的 练习 。------     缓冲流关闭 时   close() 会自动关闭  节点流   。  不用人工手动调用。
 * 
 * 
 * 对  word 文件    的   【  复制  】  使用 字节流, 不能使用字符流 ,  word中 可能会存在 图片等信息  !!!!!。
 * 
 * 
 * 
 * 
 * @author Administrator
 *
 */
public class TestBufferedInputStream {

/**
* 1. 利用   处理流     中的  缓冲流    ---- 实现      字节文件 --音频  视频   图片    的复制。
*/
@Test
public  void  testcopy(){
long  start = System.currentTimeMillis();
String src = "D:\\io\\1.wmv";
String des = "D:\\io\\1_b_copy.wmv";
this.copyZijieFile(src, des);
long  end = System.currentTimeMillis();
System.out.println("耗时:"+(end - start));  //144毫秒  -- 效率比 使用 节点流  中 字节流   快。
}




/**
* 1、利用 字节--缓冲流                     复制   图片 。  
*/
@Test
public  void testBufferedInputStream(){
// 第一步:
File file = new File("d:\\io\\1.jpg");
File dest = new File("d:\\io\\1_b_copy.jpg");
// 第三步  :把字节流 加入到 缓冲流中 提高 文件处理的效率 
BufferedInputStream bis=null;
BufferedOutputStream bos =null;
try {
// 第二步
FileInputStream  fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(dest);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);

byte[] b = new byte[1024];

int len;
while((len = bis.read(b))!=-1){
// 缓冲流 把信息 写到磁盘 
bos.write(b, 0, len);
// 最后一次的数组信息从内存请移到磁盘
bos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bis!= null){
try {
bis.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bos!= null){
try {
bos.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



}

/**
* 利用 -----字节缓冲流---复制 图片  视频 音频等字节文件。     
* @param src
* @param des
*/
public  void copyZijieFile(String src ,String des){
// 第一步:
File file = new File(src);
File dest = new File(des);
// 第三步  :把字节流 加入到 缓冲流中 提高 文件处理的效率 
BufferedInputStream bis=null;
BufferedOutputStream bos =null;
try {
// 第二步
FileInputStream  fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(dest);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);

byte[] b = new byte[1024];

int len;
while((len = bis.read(b))!=-1){
// 缓冲流 把信息 写到磁盘 
bos.write(b, 0, len);
// 最后一次的数组信息从内存请移到磁盘
bos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bis!= null){
try {
bis.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



}









}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

prefectjava

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值