演示BufferedOutputStream和BufferedInputStream
-
buffer
(1)bufferInputStream
(2)bufferOutputStream
使用它们,可以完成二进制文件拷贝,也可以操作文本文件
package com.io.outputstream_; import java.io.*; /** * 演示BufferedOutputStream和BufferedInputStream.关闭时也只需关闭外层的处理流,底层节点流会自动关闭 */ public class BufferedCopy02 { public static void main(String[] args) { String srcPath="d:\\cat.jpg"; String destPath="d:\\cat3.jpg"; BufferedInputStream bis=null; BufferedOutputStream bos=null; //创建BufferedOutputStream对象和BufferedInputStream对象 try { //因为FileInputStream是InputStream子类 bis=new BufferedInputStream(new FileInputStream(srcPath)); bos=new BufferedOutputStream(new FileOutputStream(destPath)); //循环读取文件,并写入到destPath byte[] buff=new byte[1024]; int readLen=0; //当返回-1时表示文件读取完毕 while ((readLen=bis.read(buff))!=-1){ bos.write(buff,0,readLen); } System.out.println("文件拷贝完毕"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis!=null){ bis.close(); } if (bos!=null){ bos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义