欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

使用NIO进行文件拷贝,nio文件拷贝,只要三行代码进行文件拷贝

来源: javaer 分享于  点击 41313 次 点评:54

使用NIO进行文件拷贝,nio文件拷贝,只要三行代码进行文件拷贝


只要三行代码进行文件拷贝,嘿嘿,当然三行中不包含文件是否存在的判断和异常的处理了,只是想说明一下,采用FileChannel的API的方便性。```javaimport 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);    //上面没有进行文件是否存在的判断和异常的处理}

}```

相关栏目:

用户点评