public BufferedInputStream(InputStream in) :创建一个 新的缓冲输入流。
public BufferedOutputStream(OutputStream out): 创建一个新的缓冲输出流。
1 2 3 4
//创建字节缓冲输入流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("xhh.txt")); //创建字节缓冲输出流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("xhh.txt"));
效率测试
直接使用 FileInputStream 与 FileOutStream 复制文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicstaticvoidmain(String[] args)throws Exception { long start = System.currentTimeMillis(); FileInputStream fis = new FileInputStream("D:/apache-echarts-4.6.0-incubating-src.zip"); FileOutputStream fos = new FileOutputStream("newApache.zip"); int len; byte[] b = newbyte[1024]; while ((len = fis.read(b)) != -1){ fos.write(b); } fis.close(); fos.close(); long end = System.currentTimeMillis(); System.out.println("时间:"+(end-start)); }
// 49
使用缓冲流复制文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
publicstaticvoidmain(String[] args)throws Exception { long start = System.currentTimeMillis(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/apache-echarts-4.6.0-incubating-src.zip")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("newApache.zip")); int len; byte[] b = newbyte[1024]; while ((len = bis.read(b)) != -1){ bos.write(b); } bis.close(); bos.close(); long end = System.currentTimeMillis(); System.out.println("时间:"+(end-start)); }
// 25
字符缓冲流
构造方法
public BufferedReader(Reader in)
public BufferedWriter(Writer out)
1 2 3 4
publicstaticvoidmain(String[] args)throws Exception { BufferedReader br = new BufferedReader(new FileReader("xhh.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("xhh.txt")); }
特有方法
BufferedReader
public String readLine():读一行文字
BufferedWriter
public void newLine() :写一行行分隔符,由系统属性定义符号
1 2 3 4 5 6 7 8
publicstaticvoidmain(String[] args)throws Exception { BufferedReader br = new BufferedReader(new FileReader("xhh.txt")); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); }