IO流---Buffered缓冲流
为了进一步提高IO流的效率,在IO包里提供了两个带缓冲的字节流,分别是BufferedInputStream和BufferedOutPutStream。
通过API文档的查询和学习,具体操作很简单,只需要直接在字节流操作的基础上“套上”缓冲流即可。
输入流:
1 package cn.ftf.io; 2 3 import java.io.BufferedInputStream; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.IOException; 8 import java.io.InputStream; 9 10 /** 11 * 四步骤:分段读取,文件字节输入流i,加入缓冲流 12 * 1,创建源 13 * 2,选择流 14 * 3,操作 15 * 4,通知释放资源 16 * @author 房廷飞 17 * 18 */ 19 public class TestIO06 { 20 public static void main(String[] args) { 21 //创建源 22 File src=new File("D:\\myjavacode\\JavaPractise01\\src\\cn\\ftf\\io\\abc.txt"); 23 //选择流 24 InputStream is = null; 25 BufferedInputStream bis=null; //加入缓冲流 26 //操作 27 try { 28 is=new FileInputStream(src); 29 bis=new BufferedInputStream(is); 30 /* 31 * 为了更简洁方便,选择流一般都是这样写的 32 33 //选择流 34 InputStream is = null; 35 36 is=new BufferedInputStream(new FileInputStream(src)) 37 38 //操作 39 * 40 * 最后is.close() 41 ······ 42 */ 43 byte[]flush=new byte[1024]; 44 int len=-1; 45 while((len=is.read(flush))!=-1) { 46 String str=new String(flush,0,len); 47 System.out.println(str); 48 } 49 50 } catch (FileNotFoundException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } catch (IOException e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 }finally { //关闭字节缓冲流,字节流就不用关了,自动关了 57 try { 58 if(null!=bis) 59 bis.close(); 60 } catch (IOException e) { 61 // TODO Auto-generated catch block 62 e.printStackTrace(); 63 } 64 } 65 } 66 }
文件拷贝:
1 package cn.ftf.io; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12 13 public class BufferedCopy { 14 15 public static void main(String[] args) { 16 copyFile("D:/myjavacode/JavaPractise01/src/cn/ftf/io/abc.txt","C:/Users/user/Desktop/hello.py"); 17 } 18 public static void copyFile(String first,String second) { 19 //1,创建源 20 File file1=new File(first); 21 File file2=new File(second); 22 /* 23 * java7新特性--自动关闭资源 24 * 格式 25 try( 26 //此处写打开资源的代码,此处创建的对象必须实现java.lang.AutoClosable接口 27 声明变量 变量=java.lang.AutoClosable接口的实例 28 ) 29 { 30 操作 31 } 32 catch{ 33 //处理异常的方法 34 } 35 */ 36 //选择流 37 try(InputStream src1 = new BufferedInputStream(new FileInputStream(file1)); 38 OutputStream src2 = new BufferedOutputStream(new FileOutputStream(file2));){ 39 byte[] flush=new byte[1024]; 40 try { 41 while(src1.read(flush)!=-1) { 42 src2.write(flush); 43 src2.flush(); 44 } 45 } catch (IOException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 } catch (FileNotFoundException e1) { 50 // TODO Auto-generated catch block 51 e1.printStackTrace(); 52 } catch (IOException e1) { 53 // TODO Auto-generated catch block 54 e1.printStackTrace(); 55 } 56 } 57 }
缓冲流文件拷贝程序中包含一个Java7中的新特性:
* java7新特性--自动关闭资源
* 格式
try(
//此处写打开资源的代码,此处创建的对象必须实现java.lang.AutoClosable接口
声明变量 变量=java.lang.AutoClosable接口的实例
)
{
操作
}
catch{
//处理异常的方法
}
具体实例如上面程序所示,套用这种格式可以不用关闭资源。虚拟机将自动通知系统关闭资源。

浙公网安备 33010602011771号