栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

[Java] 用字节流copy文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[Java] 用字节流copy文件

package outputstream;

import java.io.*;

public class 文件copy {
    public static void main(String[] args) {
        //完成 文件拷贝,将cc.txt 拷贝到目录19
        //思路分析
        //1.创建文件的输入流,将文件读入到程序
        //2.创建文件的输出流,将读取到的文件数据,写入到指定的位置
        String srcFilePath = "C:\Users\ASUS\Desktop\19\abc.jpg";
        String destFilePath = "C:\Users\ASUS\Desktop\19\66c.jpg";
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;

        try {
            fileInputStream =new FileInputStream(srcFilePath);
            fileOutputStream =new FileOutputStream(destFilePath);
            //定义一个字节数组,提高读取效果
            byte[] buf =new byte[1024];
            int readLen=0;
            while ((readLen=fileInputStream.read(buf))!=1){
                //读取到后,就写入到文件 通过 fileOutputStream
                //即边读边写
                fileOutputStream.write(buf,0,readLen);//一定要使用这个方法
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileInputStream!=null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream!=null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/933033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号