字节缓冲流介绍
构造方法:
| 方法名 | 说明 |
|---|---|
| BufferedOutputStream(OutputStream out) | 创建字节缓冲输出流对象 |
| BufferedInputStream(InputStream in) | 创建字节缓冲输入流对象 |
示例代码
xxxxxxxxxxpublic class BufferStreamDemo { public static void main(String[] args) throws IOException { //字节缓冲输出流:BufferedOutputStream(OutputStream out) BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myByteStream\\bos.txt")); //写数据 bos.write("hello\r\n".getBytes()); bos.write("world\r\n".getBytes()); //释放资源 bos.close(); //字节缓冲输入流:BufferedInputStream(InputStream in) BufferedInputStream bis = new BufferedInputStream(new FileInputStream("myByteStream\\bos.txt")); //一次读取一个字节数据// int by;// while ((by=bis.read())!=-1) {// System.out.print((char)by);// } //一次读取一个字节数组数据 byte[] bys = new byte[1024]; int len; while ((len=bis.read(bys))!=-1) { System.out.print(new String(bys,0,len)); } //释放资源 bis.close(); }}案例需求
把“E:\itcast\字节流复制图片.avi”复制到模块目录下的“字节流复制图片.avi”
实现步骤
代码实现
xxxxxxxxxxpublic class CopyAviDemo { public static void main(String[] args) throws IOException { //复制视频// method1(); method2(); } //字节缓冲流一次读写一个字节数组 public static void method2() throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\itcast\\字节流复制图片.avi")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myByteStream\\字节流复制图片.avi")); byte[] bys = new byte[1024]; int len; while ((len=bis.read(bys))!=-1) { bos.write(bys,0,len); } bos.close(); bis.close(); } //字节缓冲流一次读写一个字节 public static void method1() throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\itcast\\字节流复制图片.avi")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myByteStream\\字节流复制图片.avi")); int by; while ((by=bis.read())!=-1) { bos.write(by); } bos.close(); bis.close(); }}