概念
缓冲流 只是功能流的一种!功能流:不直接面对数据源,在节点流的基础上做了一定的功能扩展!
缓冲流:为了尽量的减少 节点流在操作IO时,对磁盘的大量读写导致的性能低下问题,而提供了一种缓冲能力!
根据底层包装的流的不同,分为以下两种缓冲流:
1. 字节缓冲流
BufferedInputStream 、BufferedOutputStream 提供了缓冲能力,内部都提供:8192字节大小的数组,作为:我们操作数据的缓冲空间!
public class TestBufferInputStream {
public static void main(String[] args) {
//读 E:/woniu/57/JavaSE/day19/Hello.java
File f = new File(“E:/woniu/57/JavaSE/day19/Hello.java”);
/FileInputStream fis = null;
BufferedInputStream bis = null;/
InputStream bis = null;
try {
/fis = new FileInputStream(f);
//在字节流基础上包装一个 字节缓冲流
bis = new BufferedInputStream(fis);/
bis = new BufferedInputStream(new FileInputStream(f));
//1.一个一个字节的读
/int i = bis.read();
while(i != -1){
System.out.print((char)i);
i = bis.read();
}/
//2 读一个数组
/byte[] cs = new byte[(int) f.length()];
bis.read(cs);
String s = new String(cs);
System.out.println(s);/
//3 指定数组大小
byte[] bs = new byte[100];
int len = bis.read(bs,0,bs.length);
while(len != -1){
String s = new String(bs,0,len);
System.out.print(s);
len = bis.read(bs,0,bs.length);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
bis.close();
//fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class TestBufferOutputStream {
public static void main(String[] args) {
String s = “OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集”;
//把 s 字符串 变成 字节
byte[] bytes = s.getBytes();
//输出到 E:/woniu/57/JavaSE/day19/b.txt
File f = new File(“E:/woniu/57/JavaSE/day19/b.txt”);
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(f));
//1. 按字节
/for(int i=0;i
}
//2.写出 字节数组
/out.write(bytes);
out.flush();/
//3.指定数组大小
out.write(bytes, 0, 100);
out.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
使用 字节的缓冲流 实现 文件 拷贝
2.字符缓冲流
字符在操作时,同样提供的对应的缓冲流:BufferedReader、BufferedWriter (目的:依旧是为了减少大量磁盘操作)
BufferedReader、BufferedWriter 缓冲大小依旧是:8192
public class TestBufferReader {
public static void main(String[] args) {
//E:/woniu/57/JavaSE/day19/Hello.java
File f = new File(“E:/woniu/57/JavaSE/day19/Hello.java”);
BufferedReader r = null;
try {
r = new BufferedReader(new FileReader(f));
//1.一个一个字符读
/int i = r.read();
while(i != -1){
System.out.print((char)i);
i = r.read();
}/
//2 .读到一个数组
/char[] cs = new char[(int) f.length()];
r.read(cs);
String s = new String(cs);
System.out.println(s);/
//3.指定数组大小
/char[] cs = new char[10];
int len = r.read(cs, 0, cs.length);
while(len != -1){
String s = new String(cs,0,len);
System.out.print(s);
len = r.read(cs, 0, cs.length);
}/
//4.缓冲流 还有方法 一次读一行 首选
String str = r.readLine();
while(str != null){
System.out.println(str);
str = r.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
r.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class TestBufferedWriter {
public static void main(String[] args) {
String s = “OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集”;
//E:/woniu/57/JavaSE/day19/c.txt
File f = new File(“E:/woniu/57/JavaSE/day19/c.txt”);
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Writer w = null;
try {
w = new BufferedWriter(new FileWriter(f));
//1.把字符串写出去
w.write(s);
w.flush();
//2.一个一个字符
//3.写出去 整个 字符数组
//4 .写出 指定字符数组 大小
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
w.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
使用 字符缓冲流 实现 文件 拷贝