加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

使用NIO进行文件拷贝

发布时间:2020-05-24 22:13:52 所属栏目:Java 来源:互联网
导读:使用NIO进行文件拷贝

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

只要三行代码进行文件拷贝,嘿嘿,当然三行中不包含文件是否存在的判断和异常的处理了,只是想说明一下,采用FileChannel的API的方便性。
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.nio.channels.FileChannel ;
 
public class FileCopy
{
    public static void main(String[]args) throws IOException{
        String sourcefile="E:参考资料设计模式.pdf";
        String targetfile = "E:参考资料设计模式1.pdf";
        copyfile(sourcefile,targetfile);
    }
    /**
     * 
         * 方法用途:文件拷贝
         * 方法名:copyfile
         * 返回值:void
         * 
         * 参数:@param sourcefile 源文件
         * 参数:@param targetfile 目标文件
         * 参数:@throws IOException
     */
    private static void copyfile(String sourcefile,String targetfile) throws IOException{
        FileChannel sourcefc = new FileInputStream(sourcefile).getChannel();
        FileChannel targetfc = new FileOutputStream(targetfile).getChannel();
         
        sourcefc.transferTo(0,sourcefc.size(),targetfc);
        //上面没有进行文件是否存在的判断和异常的处理
    }
}

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读