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);

    }
}
相关推荐
sino爱学习8 分钟前
高性能线程池实践:Dubbo EagerThreadPool 设计与应用
java·后端
阿猿收手吧!14 分钟前
【C++】string_view:高效字符串处理指南
开发语言·c++
风生u40 分钟前
activiti7 详解
java
玄同76544 分钟前
我的 Trae Skill 实践|使用 UV 工具一键搭建 Python 项目开发环境
开发语言·人工智能·python·langchain·uv·trae·vibe coding
岁岁种桃花儿1 小时前
SpringCloud从入门到上天:Nacos做微服务注册中心(二)
java·spring cloud·微服务
Word码1 小时前
[C++语法] 继承 (用法详解)
java·jvm·c++
Yorlen_Zhang1 小时前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
lxl13071 小时前
C++算法(1)双指针
开发语言·c++
TT哇1 小时前
【实习 】银行经理端两个核心功能的开发与修复(银行经理绑定逻辑修复和线下领取扫码功能开发)
java·vue.js
逝水如流年轻往返染尘1 小时前
Java中的数组
java