IO文件拷贝

java 复制代码
package myio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IoDemo4 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("D:\\你的文件路径\\b.txt");
        FileOutputStream fos = new FileOutputStream("D:\\你的文件路径\\copy.txt");
        int b;
        while (( b = fis.read()) != -1){
            fos.write(b);
        }
        fis.close();
        fos.close();
    }
}

弊端和解决方法

FileInputStream一次只能读取一个字节,速度太慢

如果文件过大可以使用多个字节读取

java 复制代码
package myio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IoDemo4 {
    public static void main(String[] args) throws IOException {
        long sta = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("D:\\你的文件路径\\ceshi.mp3");
        FileOutputStream fos = new FileOutputStream("D:\\你的文件路径\\copy.mp3");
        int len;
        byte[] bytes = new byte[1024 * 1024 * 5];
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
        fis.close();
        fos.close();
        long end = System.currentTimeMillis();
        System.out.println(end - sta);

    }
}
相关推荐
en.en..几秒前
Linux 进程退出exit(函数)
java·开发语言·算法
李游Leo2 分钟前
HarmonyOS 7 TaskPool + Node-API + zstd:Native 批量压缩任务的主线程隔离、进度回传与资源收口【鸿蒙心迹】
java·华为·harmonyos
Cx330❀3 分钟前
【Qt 进阶指南】详解 Qt 常用标准对话框与主窗口组件架构
开发语言·qt·ui·性能优化·架构·图形渲染
Light Gao3 分钟前
Encoder 与 Decoder 详解:从 Embedding 到 Seq2Seq 的完整数据流
java·开发语言·embedding
HZZD_HZZD4 分钟前
弱网下CoAP还省吗?合众致达Cat.1电表90天实测:MQTT重连流量占52%、续航差距从38%拉大到73%
开发语言·物联网·php·腾讯云
web打印社区8 分钟前
Windows 网页静默打印设置步骤:客户端、防火墙与联调清单
开发语言·前端·javascript·chrome·pdf·ecmascript
小师兄吃牛肉17 分钟前
什么是R语言?如何快速学习R语言
开发语言·学习·r语言
程序猿乐锅31 分钟前
【黑马点评 | 第十一篇】关注 Feed 流实现
java·数据库·redis·分布式·后端·缓存·maven
Evand J39 分钟前
【MATLAB例程】三维快速扩展随机树(RRT)路径规划与到达角(AOA)、到达时间差(TDOA)融合定位,附下载链接
开发语言·matlab·代码·定位·例程
执明wa41 分钟前
Android RecyclerView 实战:点击频道栏切换对应内容
android·开发语言·windows·microsoft·android studio