s-day09--字节缓冲流的基本使用以及使用缓冲流复制文件(一次读写一个字节
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
package com.inmind.buffered01;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
/*
|
||||||
|
3.字节缓冲流的基本使用以及使用缓冲流复制文件(一次读写一个字节)
|
||||||
|
字节缓冲输入流:BufferedInputStream
|
||||||
|
构造方法:
|
||||||
|
BufferedInputStream(InputStream in) 创建一个 BufferedInputStream并保存其参数,输入流 in供以后使用。
|
||||||
|
常用方法:
|
||||||
|
int read() :读取一个字节数据
|
||||||
|
int read(byte[] bytes) : 读取一个字节数组
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class BufferedDemo01 {
|
||||||
|
|
||||||
|
//69ms
|
||||||
|
public static void main(String[] args)throws Exception{
|
||||||
|
//直接使用字节缓冲输入输出流,复制一个文件,检查它的时间消耗
|
||||||
|
FileInputStream input = new FileInputStream("a.jpg");
|
||||||
|
FileOutputStream output = new FileOutputStream("b2.jpg");
|
||||||
|
BufferedInputStream fis = new BufferedInputStream(input);
|
||||||
|
BufferedOutputStream fos = new BufferedOutputStream(output);
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
|
int c;
|
||||||
|
while ((c = fis.read()) != -1) {
|
||||||
|
fos.write(c);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
fis.close();
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
System.out.println("复制的时间消耗:"+(end -start));
|
||||||
|
}
|
||||||
|
|
||||||
|
//7360ms
|
||||||
|
public static void method(String[] args) throws IOException {
|
||||||
|
//直接使用字节输入输出流,复制一个文件,检查它的时间消耗
|
||||||
|
FileInputStream fis = new FileInputStream("a.jpg");
|
||||||
|
FileOutputStream fos = new FileOutputStream("b1.jpg");
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
|
||||||
|
int c;
|
||||||
|
while ((c = fis.read()) != -1) {
|
||||||
|
fos.write(c);
|
||||||
|
}
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
fis.close();
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
System.out.println("复制的时间消耗:"+(end -start));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user