0
点赞
收藏
分享

微信扫一扫

字节缓冲流(BufferedOutputStream,BufferedInputStream)



这个连接包含了常用的流------IO流(总篇章)  



字节缓冲流


BufferedOutputStream:该类实现缓冲输出流。通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用BufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组。当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,有一次很多字节




构造方法:


  • 字节缓冲输出流:BufferedOutputStream(OutputStream out)
  • 字节缓冲输入流:BufferedInputStream(InputStream in)



为什么构造方法需要的是字节流,而不是具体的文件或者路径呢?

  • 字节缓冲流​仅仅提供缓冲区,​而真正的读写数据还得依靠基本的字节流对象进行操作


package com.testIO;

import java.io.*;

/**
* @author 林高禄
* @create 2020-05-09-15:51
*/
public class BufferStreamDemo {
public static void main(String[] args) throws IOException{
copyAvi();
}

public static void copyAvi() throws IOException{
fileByte();
fileBytes();
bufferByte();
bufferBytes();
}
// 基本字节流读写一个字节
public static void fileByte ()throws IOException {
long startTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("test//src//com//testIO//字节流复制视频.avi");
FileOutputStream fos = new FileOutputStream("test//src//com//testIO//字节流复制视频a.avi");
int by;
while ((by=fis.read())!= -1){
fos.write(by);
}
fos.close();
fis.close();
long sendTime = System.currentTimeMillis();
System.out.println("基本字节流读写一个字节共耗时:"+(sendTime-startTime)+"毫秒");
}
// 基本字节流读写一个字节数组
public static void fileBytes ()throws IOException {
long startTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream("test//src//com//testIO//字节流复制视频.avi");
FileOutputStream fos = new FileOutputStream("test//src//com//testIO//字节流复制视频b.avi");
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys))!= -1){
fos.write(bys,0,len);
}
fos.close();
fis.close();
long sendTime = System.currentTimeMillis();
System.out.println("基本字节流读写一个字节数组共耗时:"+(sendTime-startTime)+"毫秒");
}
// 字节缓冲流读写一个字节
public static void bufferByte ()throws IOException {
long startTime = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test//src//com//testIO//字节流复制视频.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test//src//com//testIO//字节流复制视频c.avi"));
int by;
while ((by=bis.read())!= -1){
bos.write(by);
}
bos.close();
bis.close();
long sendTime = System.currentTimeMillis();
System.out.println("字节缓冲流读写一个字节共耗时:"+(sendTime-startTime)+"毫秒");
}
// 字节缓冲流读写一个字节数组
public static void bufferBytes ()throws IOException {
long startTime = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test//src//com//testIO//字节流复制视频.avi"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test//src//com//testIO//字节流复制视频d.avi"));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!= -1){
bos.write(bys,0,len);
}
bos.close();
bis.close();
long sendTime = System.currentTimeMillis();
System.out.println("字节缓冲流读写一个字节数组共耗时:"+(sendTime-startTime)+"毫秒");
}
}


输出结果:

基本字节流读写一个字节共耗时:171094毫秒
基本字节流读写一个字节数组共耗时:274毫秒
字节缓冲流读写一个字节共耗时:789毫秒
字节缓冲流读写一个字节数组共耗时:109毫秒



从输出结果我们可以看出,字节缓冲流读写一个字节数组的效率比较高!




举报

相关推荐

0 条评论