如何使用java代码复制文件呢?

戚薇 Java经验 发布时间:2023-05-30 22:39:08 阅读数:11940 1
下文笔者讲述java代码复制文件的最简介的方法分享,如下所示

复制文件的实现思路

借助FileInputStream和FileOutputStream 可实现文件复制的效果
例:文件快捷复制的示例
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if(!destfile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    }
    finally {
        if(source != null) {
            source.close();
        }
        if(destination != null) {
            destination.close();
        }
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202305/16854575846663.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者