缓冲流是处理流的一种html
处理流就是“套接”在已有的流的基础上java
字节:数组
字符:code
缓冲流的主要做用是提升文件的读写效率视频
字节型htm
实例化File类的对象,指明要操做的文件对象
提供具体的流blog
2.1 造节点流get
2.2 造缓冲流input
数据的写出输入过程
流的关闭操做
try-catch-finally异常处理
FileInputStream fis = null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //1. 实例化File类的对象,指明要操做的文件 File srcFile = new File("C:\\Users\\LENOVO\\Desktop\\丸子.png"); File destFile = new File("C:\\Users\\LENOVO\\Desktop\\殷志源.png"); //2. 提供具体的流 //2.1 造节点流 fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //2.2 造缓冲流 bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); //3. 读取和写入过程 byte[] buffer = new byte[10]; int len; while((len = bis.read(buffer)) != -1 ){ bos.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4. 关闭 try { if(bis != null) bis.close(); } catch (IOException e) { e.printStackTrace(); } try { if(bos != null) bos.close(); } catch (IOException e) { e.printStackTrace(); } //关闭内层流省略 // try { // if(fos != null) // fos.close(); // } catch (IOException e) { // e.printStackTrace(); // } // // try { // if(fis != null) // fis.close(); // } catch (IOException e) { // e.printStackTrace(); // } }
两次代码的byte[]数组设定的长度相同,都是byte[1024],复制的都是同一个视频
利用节点流花费的时间为339(在节点流章节2.2中能够查看)
http://www.javashuo.com/article/p-ndkwhcha-mx.html
利用缓冲流花费的时间为77
其内部提供了一个缓冲区
文件在读取的时候先将文件读取到缓冲区,达到缓冲区指定大小后一次性的写出
BufferedOutputStream中有一个方法
bos.flush; //刷新缓冲区(会自动刷新,不用显式调用)
超出缓冲区指定的大小的时候,会自动的刷新出去(把缓冲区的数据状况,将数据写出去)
和BufferedInputStream和BufferedOutputStream用法类似
BufferedInputStream和BufferedOutputStream“包”的是FileInputStream和FileOutputStream
BufferedReader和BufferedWriter“包”的是FileReader和FileWriter
具体缘由见下图( http://www.javashuo.com/article/p-ewtsqfat-mx.html )
读操做的步骤中的两种写法
char[] cbuf = new char[1024]; int len; while((len = bfr.read(cbuf)) != -1){ bfw.write(cbuf,0,len); }
String data; while((data = bfr.readLine()) != null){ bfw.write(data + "\n"); }
bfw.write(data );
bfw.write(data); bfw.newLine();
文本文件的复制
public void copytxt(String srcPath, String destPath){ BufferedReader bfr = null; BufferedWriter bfw = null; try { File srcFile = new File(srcPath); File destFile = new File(destPath); bfr = new BufferedReader(new FileReader(srcFile)); bfw = new BufferedWriter(new FileWriter(destFile)); //方式一 // char[] cbuf = new char[1024]; // int len; // while((len = bfr.read(cbuf)) != -1){ // bfw.write(cbuf,0,len); // } //方式二 String data; while((data = bfr.readLine()) != null){ //2.1 // bfw.write(data + "\n");//data中不包含换行符 //2.2 bfw.write(data); bfw.newLine(); } System.out.println("复制成功"); } catch (IOException e) { e.printStackTrace(); } finally { try { if(bfr != null) bfr.close(); } catch (IOException e) { e.printStackTrace(); } try { if(bfw != null) bfw.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test6(){ String srcPath = "C:\\Users\\LENOVO\\Desktop\\hello.txt"; String destPath = "C:\\Users\\LENOVO\\Desktop\\hello(1).txt"; long start = System.currentTimeMillis(); copytxt(srcPath,destPath); long end = System.currentTimeMillis(); System.out.println("花费的时间:" + (end - start)); }