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

文件拷贝-代码

文件拷贝的基本代码:

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.dtcms.com/a/313362.html

相关文章:

  • Doris json_contains 查询报错
  • 数据结构总纲以及单向链表详解:
  • 【LeetCode刷题指南】--对称二叉树,另一颗树的子树
  • [创业之路-531]:知识、技能、技术、科学之间的区别以及它们对于职业的选择的指导作用?
  • 【OpenGL】LearnOpenGL学习笔记02 - 绘制三角形、矩形
  • 13-day10生成式任务
  • 基于MBA与BP神经网络分类模型的特征选择方法研究(Python实现)
  • 在ANSYS Maxwell中对永磁体无线充电进行建模
  • 【大模型核心技术】Agent 理论与实战
  • 【设计模式】5.代理模式
  • Manus AI与多语言手写识别
  • 什么是“痛苦指数”(Misery Index)?
  • 如何获取网页中点击按钮跳转后的链接呢
  • 在 Cursor 中设置浅色背景和中文界面
  • 抽奖系统中 Logback 的日志配置文件说明
  • 03.一键编译安装Redis脚本
  • 【MySQL】MySQL 中的数据排序是怎么实现的?
  • 深入理解流式输出:原理、应用与大模型聊天软件中的实现
  • 跨语言模型中的翻译任务:XLM-RoBERTa在翻译任务中的应用
  • python---python中的内存分配
  • AI Agent 重塑产业发展新格局
  • 联想笔记本安装系统之后一直转圈圈的问题了?无法正常进入到系统配置界面,原来是BIOS中的VMD问题
  • Autoswagger:揭露隐藏 API 授权缺陷的开源工具
  • 使用CMake构建项目的完整指南
  • [LINUX操作系统]shell脚本之循环
  • 【Qt】QObject::startTimer: Timers cannot be started from another thread
  • 如何玩转 Kubernetes K8S
  • 【QT】概述
  • 快速搭建一个非生产k8s环境
  • Android 之 网络通信(HTTP/TCP/UDP/JSON)