缓冲流
开发工具与关键技术:eclipse java
作者:梁峻豪
缓冲流
在我们学习字节流与字符流的时候,大家都进行过读取文件中数据的操作,读取数据
量大的文件时,读取的速度会很慢,很影响我们程序的效率,那么,我想提高速度,怎么办?
Java 中提高了一套缓冲流,它的存在,可提高 IO 流的读写速度
缓冲流,根据流的分类分类字节缓冲流与字符缓冲流。
字节缓冲流
字节缓冲流根据流的方向,共有 2 个
写入数据到流中,字节缓冲输出流 BufferedOutputStream
读取流中的数据,字节缓冲输入流 BufferedInputStream
它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了 IO 流的读写速度
字节缓冲输出流 BufferedOutputStream
通过字节缓冲流,进行文件的读写操作 写数据到文件的操作
构造方法public BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
public class BufferedOutputStreamDemo01 { public static void main(String[] args) throws
IOException { //写数据到文件的方法 write(); } /* * 写数据到文件的方法
- 1,创建流 * 2,写数据 * 3,关闭流 / private static void write() throws
IOException { //创建基本的字节输出流 FileOutputStream fileOut = new
FileOutputStream(“abc.txt”); //使用高效的流,把基本的流进行封装,实现速度的提升
BufferedOutputStream out = new BufferedOutputStream(fileOut); //2,写数据
out.write(“hello”.getBytes()); //3,关闭流 out.close(); } }
字节缓冲输入流 BufferedInputStream
刚刚我们学习了输出流实现了向文件中写数据的操作,那么,现在我们完成读取文件中数据的操作
构造方法
public BufferedInputStream(InputStream in)
/ * 从文件中读取数据 * 1,创建缓冲流对象 * 2,读数据,打印 * 3,关闭
/ private static void read() throws IOException { //1,创建缓冲流对象
FileInputStream fileIn = new FileInputStream(“abc.txt”); //把基本的流包装成高效的
流 BufferedInputStream in = new BufferedInputStream(fileIn); //2,读数据 int ch =
-1; while ( (ch = in.read()) != -1 ) { //打印System.out.print((char)ch); } //3,关闭 in.close(); }
使用基本的流与高效的流完成复制文件
我们一直在说,高效的流速度快并高效,怎么体现呢?需要通过一个复制文件耗时的比较
过程,来体验一下高效流带来的快感。
/ * 需求:将 d:\test.avi 文件进行复制 * 采用 4 种方式复制 * 方式 1: 采用基本的流
,一次一个字节的方式复制 共耗时 224613 毫秒 * 方式 2: 采用基本的流,一个多个字
节的方式赋值 共耗时 327 毫秒 * 方式 3: 采用高效的流,一次一个字节的方式复制
共耗时 2047 毫秒 * 方式 4: 采用高效的流,一个多个字节的方式赋值 共耗时 96 毫
秒 * * 数据源: d:\test.avi * 目的地 1: d:\copy1.avi * 目的地 2: d:\copy2.avi * 目的地 3:
d:\copy3.avi * 目的地 4: d:\copy4.avi * * 实现的步骤: * 1,指定数据源 * 2,指定目的地 - 3,读数据 * 4,写数据 * 5,关闭流 * */public class CopyAVI { public static void
main(String[] args) throws IOException { //开始计时 long start =
System.currentTimeMillis(); //方式 1: 采用基本的流,一次一个字节的方式复制
//method1(“d:\test.avi”, “d:\copy1.avi”); //方式 2: 采用基本的流,一个多个字节
的方式赋值 //method2(“d:\test.avi”, “d:\copy2.avi”); //方式 3: 采用高效的流,一
次一个字节的方式复制 //method3(“d:\test.avi”, “d:\copy3.avi”); //方式 4: 采用高
效的流,一个多个字节的方式赋值 method4(“d:\test.avi”, “d:\copy4.avi”); //结
束计时 long end = System.currentTimeMillis(); //打印耗时多少毫秒
System.out.println("共耗时 " +(end - start)+ “毫秒”); } //方式 4: 采用高效的流,
一个多个字节的方式赋值 private static void method4(String src, String dest) throws
IOException { //1,指定数据源 BufferedInputStream in = new
BufferedInputStream(new FileInputStream(src)); //2,指定目的地
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(dest)); //3,读数据 byte[] buffer = new byte[1024]; int len = -
1; while ( (len = in.read(buffer)) != -1) { //4,写数据 out.write(buffer, 0,
len); } //5,关闭流 in.close(); out.close(); } //方式 3: 采用高效的流,
一次一个字节的方式复制 private static void method3(String src, String dest) throws
IOException { //1,指定数据源 BufferedInputStream in = new
BufferedInputStream(new FileInputStream(src)); //2,指定目的地
BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(dest)); //3,读数据 int ch = -1; while ((ch=in.read()) != -1)
{ //4,写数据 out.write(ch); } //5,关闭流
in.close(); out.close(); } //方式 2: 采用基本的流,一个多个字节的方式赋值
private static void method2(String src, String dest) throws IOException { //1,指定数
据源 FileInputStream in = new FileInputStream(src); //2,指定目的地
FileOutputStream out = new FileOutputStream(dest); //3,读数据 byte[] buffer =
new byte[1024]; int len = -1; while ( (len=in.read(buffer)) != -1) { //4,写数据
out.write(buffer, 0, len); } //5,关闭流 in.close(); out.close(); } //
方式 1: 采用基本的流,一次一个字节的方式复制 private static void method1(String src,
String dest) throws IOException { //1,指定数据源 FileInputStream in = new
FileInputStream(src); //2,指定目的地 FileOutputStream out = new
FileOutputStream(dest); //3,读数据 int ch = -1; while (( ch=in.read()) != -1)
{ //4,写数据 out.write(ch); } //5,关闭流
in.close(); out.close(); } }