缓冲流
1.概述
缓冲流,也叫高效流,是对4个基本的 FileXxx 流的增强,所以也是4个流,按照数据类型分类:
字节缓冲流: BufferedInputStream , BufferedOutputStream
字符缓冲流: BufferedReader , BufferedWriter
缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO
次数,从而提高读写的效率。
2.字节缓冲流构造方法
public BufferedInputStream(InputStream in) :创建一个 新的缓冲输入流。
public BufferedOutputStream(OutputStream out) : 创建一个新的缓冲输出流。
3.字节输出缓冲流
作用:底层定义了一个长度为8192的数组,用来提高效率
常用方法:
public void close(): 关闭流,释放资源
public void write(int b): 写出一个字节的
public void write(byte[] bs): 写出一个字节数组的
public void write(byte[] bs,int off,int len): 写出一个字节数组的一部分
参数:
byte[] bs: 字节数组
int off: 起始索引
int len: 写出字节的个数
注意事项:
对于缓冲流,千万不要自己调用flush方法
只需要关闭缓冲流,自动关闭缓冲流所关联的其它流对象
package huanchong;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day06\\src\\huanchong\\a.txt"));
bos.write('a');
bos.write("efgh".getBytes());
bos.close();
}
4.字节输入缓冲流
package huanchong;
import java.io.*;
public class Demo01 {
public static void main(String[] args) throws IOException {
//method01();
method02();
}
//字节数组读取
private static void method02() throws IOException {
BufferedInputStream bif = new BufferedInputStream(new FileInputStream("day06\\src\\zifu\\a.txt"));
int len = 0;
byte [] bs = new byte[2];
while((len = bif.read(bs)) != -1){
System.out.println(new String(bs,0,len));
}
bif.close();
}
//单字节读取
private static void method01() throws IOException {
BufferedInputStream bif = new BufferedInputStream(new FileInputStream("day06\\src\\zifu\\a.txt"));
int b = 0;
while ((b = bif.read()) != -1){
System.out.println((char)b);
}
bif.close();
}
}
5.字节缓冲流复制文件
package huanchong;
import java.io.*;
public class Demo02 {
public static void main(String[] args) throws IOException {
File f1 = new File("day07\\src\\huanchong\\ThesisTypesettingSoft_Pro.zip");
File f2 = new File("day07\\src\\cun\\ThesisTypesettingSoft_Pro.zip");
long start = System.currentTimeMillis();
//bufferCopy01(f1,f2);//143
bufferCopy02(f1,f2);//11
long end = System.currentTimeMillis();
System.out.println("运行时间:" + (end - start));
}
//字节数组复制
private static void bufferCopy02(File f1, File f2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2));
int len = 0;
byte [] bs = new byte[1024 * 8];
while((len = bis.read(bs)) != -1){
bos.write(bs,0,len);
}
bos.close();
bis.close();
}
//单字节复制
private static void bufferCopy01(File f1, File f2) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f1));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f2));
int b = 0;
while((b = bis.read()) != -1){
bos.write(b);
}
bos.close();
bis.close();
}
}
6.字符输出缓冲流
public BufferedReader(Reader in) :创建一个 新的缓冲输入流。
public BufferedWriter(Writer out) : 创建一个新的缓冲输出流。
特有方法
BufferedReader: public String readLine() : 读一行文字。
BufferedWriter: public void newLine() : 写一行行分隔符,由系统属性定义符号。
package huanchong;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Demo03 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("day07\\src\\huanchong\\bw.txt"));
bw.write("你好");
bw.newLine();//自适应换行
bw.write("你好");
bw.newLine();
bw.write("你好");
bw.close();
}
}
7.字符输入缓冲流
package huanchong;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo04 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day07\\src\\huanchong\\bw.txt"));
String a = null;
while ((a = br.readLine()) != null){
System.out.println(a);
}
br.close();
}
}
8.文本排序练习
package huanchong;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo05 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day07\\src\\huanchong\\csbnosort.txt"));
Map<Integer, String> map = new HashMap<>();
String a = null;
while ((a = br.readLine()) != null){
String [] arr = a.split("\\.");
map.put(Integer.parseInt(arr[0]),arr[1]);
}
Set<Integer> key = map.keySet();
ArrayList<Integer> list = new ArrayList<>(key);
BufferedWriter bw = new BufferedWriter(new FileWriter("day07\\src\\huanchong\\csbsorted.txt"));
for (Integer mykey:list) {
bw.write(mykey + "." + map.get(mykey));
bw.newLine();
}
bw.close();
br.close();
}
}