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

    }
}
相关推荐
qfljg3 小时前
OpenRewrite 实战指南
java
Sylvia33.3 小时前
LOL实时数据接入深度解析:从WebSocket到完整数据模型
java·网络·python·websocket·网络协议
2601_962071578 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
zww89491118 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·eclipse
淡海水9 小时前
07-04-并发-ConcurrentBag-T-工作窃取WorkStealing算法
开发语言·算法·c#·bag·concurrent·workstealing
CS_Zero10 小时前
C语言sizeof是函数吗?
c语言·开发语言
-今昭-10 小时前
《V2V 迁移实战:将 VMware 虚拟机转为 OpenStack 可用 Glance qcow2 镜像》
开发语言·python
zww894911111 小时前
匿名树洞系统开发实战:从需求分析到部署指南
java·eclipse
黑马程序员毕设12 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
木井巳12 小时前
【BFS/DFS 解决 FloodFill 算法】太平洋大西洋水流问题
java·算法·leetcode·深度优先·广度优先·宽度优先·推荐算法