java中IO流-缓冲流(字节型)复制操作
import java.io.*; public class BufferedTest { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; long start = System.currentTimeMillis(); try { // 1.实例化File类的对象,指明要操作的文件 File srcFile = new File("飘窗.jpg"); File destFile = new File("飘窗1.jpg"); // 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[1024]; int len; while ((len = bis.read(buffer)) != -1) { bos.write(buffer, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally {// 4.关闭资源 // 要求:先关闭外层的流,再关闭内层的流 if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } // 可以省略(关闭外层流的同时,内层流也会自动的进行关闭) // if (fos != null) { // try { // fos.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // if (fis != null) { // try { // fis.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } } long end = System.currentTimeMillis(); System.out.println("复制花费的时间为:" + (end - start)); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!