这个连接包含了常用的流------IO流(总篇章)
字节缓冲流
构造方法:
- 字节缓冲输出流: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毫秒
从输出结果我们可以看出,字节缓冲流读写一个字节数组的效率比较高!