|NO.Z.00080|——————————|BigDataEnd|——|Java&IO流.V07|——|Java.v07|IO流.v07|缓冲字节流|实现文件拷贝|
一、BufferedOutputStream类(重点)
### --- 基本概念
——> java.io.BufferedOutputStream类主要用于描述缓冲输出流,
——> 此时不用为写入的每个字节调用底层系统。
二、常用的方法
方法声明 | 功能介绍 |
BufferedOutputStream(OutputStream out) | 根据参数指定的引用来构造对象 |
BufferedOutputStream (OutputStream out, intsize) |
根据参数指定的引用和缓冲区大小来构造对象 |
void write(int b) | 写入单个字节 |
void write(byte[] b, int off, int len) | 写入字节数组中的一部分数据 |
void write(byte[] b) | 写入参数指定的整个字节数组 |
void flush() | 刷新流 |
void close() | 关闭流对象并释放有关的资源 |
三、BufferedInputStream类(重点)
### --- 基本概念
——> java.io.BufferedInputStream类主要用于描述缓冲输入流。
四、常用的方法
方法声明 | 功能介绍 |
BufferedInputStream(InputStream in) | 根据参数指定的引用构造对象 |
BufferedInputStream(InputStream in, int size) | 根据参数指定的引用和缓冲区大小构造对象 |
int read() | 读取单个字节 |
int read(byte[] b, int off, int len) | 读取len个字节 |
int read(byte[] b) | 读取b.length个字节 |
void close() | 关闭流对象并释放有关的资源 |
五、编程代码
package com.yanqi.task17;
import java.io.*;
public class BufferedByteCopyTest {
public static void main(String[] args) {
// 获取当前系统时间距离1970年1月1日0时0分0秒的毫秒数
long g1 = System.currentTimeMillis();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.创建BufferedInputStream类型的对象与d:/02_IO流的框架结构.mp4文件关联
bis = new BufferedInputStream(new FileInputStream("d:/02_IO流的框架结构.mp4"));
// 2.创建BufferedOuputStream类型的对象与d:/IO流的框架结构.mp4文件关联
bos = new BufferedOutputStream(new FileOutputStream("d:/IO流的框架结构.mp4"));
// 3.不断地从输入流中读取数据并写入到输出流中
System.out.println("正在玩命地拷贝...");
byte[] bArr = new byte[1024];
int res = 0;
while ((res = bis.read(bArr)) != -1) {
bos.write(bArr, 0, res);
}
System.out.println("拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭流对象并释放有关的资源
if (null != bos) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bis) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
long g2 = System.currentTimeMillis();
System.out.println("使用缓冲区拷贝视频文件消耗的时间为:" + (g2-g1)); // 44
}
}
六、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=53476:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task17.BufferedByteCopyTest
正在玩命地拷贝...
拷贝文件成功!
使用缓冲区拷贝视频文件消耗的时间为:2
Process finished with exit code 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通