1 . 提供了缓存区,提高了效率
2 . 缓冲流不直接与数据源打交道,与流打交道,则缓冲流为过滤流,或者处理流.
3 . 缓冲字节输入流,就是处理字节输入流的.要记住
BufferedInputStream bis = null;
//注意 new 的时候
bis = new BufferedInputStream(new FileInputStream(new File("abc.txt")));
package com.qf.demo2;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
/**
* FileInputStream
* FileOutputStream
* FileReader
* FileWriter
*
* 都是文件流, 节点流 ,
*
*
* 过滤流(处理流)
*缓冲流
*BufferedInputStream
*BufferedOutputSteam
*BufferedReader
*BufferedWriter
*
* 字节 1024b -->1 kb
* 1024kb --> 1 Mb
* 1024Mb --> 1G
* 1024G --> 1T
*在内存中提供了 一个 8kb 的缓冲区, 提高了 执行效率
*
*
*
*/
public class Test {
public static void main(String[] args) {
BufferedInputStream bis = null;
try {
//注意 new 的时候
bis = new BufferedInputStream(new FileInputStream(new File("abc.txt")));
byte[] bs= new byte[1024];
//读的数据放进bs数组里面
int num = bis.read(bs);
int num2 = bis.read(bs);
System.out.println(num2);
System.out.println(Arrays.toString(bs));
bis.read();
bis.read(bs, 0, 4);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
4 . 缓冲字节输出流,就是处理字节输出流的.要记住
5 . 将字符串转化为byte数组
"abcdef".getBytes()
BufferedOutputStream bos = null;
bos = new BufferedOutputStream(new FileOutputStream(new File("ww.txt")));
package com.qf.demo2;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File("ww.txt")));
bos.write(new byte[]{100,97,98,99});
bos.write(101);
bos.write("abcdef".getBytes(), 2, 4);
bos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
6 . 文件字节流,读取数据的时候,会读出来对应的byte类型.
反过来,如果想利用文件输出流写入数据的时候,直接写数组,它会自动转换成对应的类型.比如,write(97) 则在文本中显示的是a.
7 . 需求
用文件流 复制文件 ,计算 耗时
用缓冲流 复制同样的文件 ,计算耗时
来证明用缓冲流操作字节流会比字节流单独处理数据要速度快.
获取系统运行当前
long item2 = System.currentTimeMillis();
package com.qf.demo2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 1 用文件流 复制文件 计算 耗时
* 2 用缓冲流 复制同样的文件 计算耗时
*
* @author Administrator
*
*/
public class Test3 {
public static void main(String[] args) {
String string = "C:\\Users\\Administrator\\Desktop\\第十七天\\第十七天视频\\第十七天上午.wmv";
File file = new File(string);
fileCopy(file);
bufferedCopy(file);
}
public static void fileCopy(File file){
long time = System.currentTimeMillis();
FileInputStream fis =null;
FileOutputStream fos =null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(new File("D:\\shipin.wmv"));
// 2 读
byte[] bs = new byte[1024];
int num =0;
while((num = fis.read(bs))!=-1){
fos.write(bs, 0, num);
fos.flush();
}
long time2 = System.currentTimeMillis();
System.out.println("文件流复制完成,耗时"+(time2-time));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void bufferedCopy(File file){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
long time = System.currentTimeMillis();
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\aaa.wmv")));
byte[] bs = new byte[1024];
int num = 0;
while((num = bis.read(bs))!=-1){
bos.write(bs, 0, num);
bos.flush();
}
long item2 = System.currentTimeMillis();
System.out.println("缓冲流 复制完毕,"+(item2-time));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}