当前位置: 首页 > java >正文

文件拷贝-代码

文件拷贝的基本代码:

public class ByteStreamDemo03 {public static void main(String[] args) throws IOException {//创建对象FileInputStream fis = new FileInputStream("D:\\itheima\\movie.mp4");FileOutputStream fos = new FileOutputStream("copy.mp4");//拷贝//核心思想:边读边写int b;while ((b= fis.read()) != -1){fos.write(b);}//先开的后释放fos.close();fis.close();}}

文件拷贝的弊端和解决方法:

弊端:一次读取一个字节

解决方法:一次读取多个字节

public class ByteStreamDemo04 {public static void main(String[] args) throws IOException {/*public int read(byte[] buffer)   一次读一个字节数组数据*/FileInputStream fis = new FileInputStream("a.txt");byte[] bytes = new byte[2];int len1 = fis.read(bytes);System.out.println(len1);String str1 = new String(bytes,0,len1);System.out.println(str1);int len2 = fis.read(bytes);System.out.println(len2);String str2 = new String(bytes,0,len2);System.out.println(str2);int len3 = fis.read(bytes);System.out.println(len3);String str3 = new String(bytes,0,len3);System.out.println(str3);fis.close();}
}

拷贝改写(一次读多个字节):

public class ByteStreamDemo05 {public static void main(String[] args) throws IOException {long start = System.currentTimeMillis();FileInputStream fis = new FileInputStream("D:\\itheima\\movie.mp4");FileOutputStream fos = new FileOutputStream("copy2.mp4");int len;byte[] bytes = new byte[1024 * 1024 * 5];while ((len = fis.read(bytes)) != -1){fos.write(bytes,0,len);}fos.close();fis.close();long end= System.currentTimeMillis();System.out.println(end - start);}
}
http://www.xdnf.cn/news/17047.html

相关文章:

  • [Oracle] 获取系统当前日期
  • 大白话讲解MCP
  • 7.28-8.3周报
  • 8月3日星期日今日早报简报微语报早读
  • 机器学习之决策树(二)
  • Leetcode:1.两数之和
  • 【C++】面向对象编程:继承与多态的魅力
  • Node.js 服务可以实现哪些功能
  • ethtool,lspci,iperf工具常用命令总结
  • 时间戳转换器
  • vector<int> adjList[MAX] 和 vector<int> adjList(MAX)的区别【C++】
  • 【Linux系统】进程间通信:匿名管道
  • UE5的渲染Debug技巧
  • 块三角掩码(Block-Triangular Masking)
  • Java 中也存在类似的“直接引用”“浅拷贝”和“深拷贝”
  • feign日志学习记录
  • k8s+isulad 国产化技术栈云原生技术栈搭建1-VPC
  • VUE-第二季-01
  • python批量gif图片转jpg
  • 【DL学习笔记】深入学习tenser
  • Claude Code入门学习笔记(一)--Claude Code简介
  • ICCV 2025 | EPD-Solver:西湖大学发布并行加速扩散采样算法
  • 多线程异步日志系统与实现及 TCP/IP C/S 模型
  • 解剖 .NET 经典:从 Component 到 BackgroundWorker
  • AD方案(OpenLDAP或微软AD)适配信创存在的不足以及可能优化方案
  • Redis面试精讲 Day 9:Redis模块开发与扩展
  • 【数据迁移】Windows11 下将 Ubuntu 从 C 盘迁移到 D 盘
  • 每日面试题20:spring和spring boot的区别
  • Spring MVC 九大组件源码深度剖析(一):MultipartResolver - 文件上传的幕后指挥官
  • Go语言实战案例:TCP服务器与客户端通信