文章目录
缓冲流概述
缓冲流也称为高效流、或者高级流。
缓冲流自带缓冲区、可以提高原始字节流、字符流读写数据的性能
BufferedInputStream、BufferedOutputStream
从构造器中可以看出,构建一个字节缓冲输入流需要一个字节输入流
使用缓冲流复制文件
public class Test2 {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("test1.mp4");
OutputStream os = new FileOutputStream("out.mp4");
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] bytes = bis.readAllBytes();
bos.write(bytes);
}
}
字节缓冲流性能分析
字符缓冲流
BufferedReader (继承了Reader)
除了Reader中的方法,提供了独有的readLine()方法
BufferedWriter (继承了Writer)
除了Writer里面的方法,提供了独有newLine()方法
按照序号复制到新的文件
package com.zsw.iotest;
import java.io.*;
import java.nio.Buffer;
import java.util.Arrays;
import java.util.Comparator;
public class Test3 {
public static void main(String[] args) {
String s;
String[] strings = new String[0];
try(BufferedReader br = new BufferedReader(new FileReader("test.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("testout.txt"))) {
while ((s = br.readLine()) != null){
strings = Arrays.copyOf(strings,strings.length+1);
strings[strings.length-1]=s;
}
Arrays.sort(strings, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Integer.parseInt(String.valueOf(o1.charAt(0)))-Integer.parseInt(String.valueOf(o2.charAt(0)));
}
});
System.out.println(Arrays.toString(strings));
for(String t :strings ){
bw.write(t);
bw.newLine();
}
}catch (Exception e){
e.getStackTrace();
}
}
}
结果: