使用NIO进行文件拷贝
2018-07-20 来源:open-open
只要三行代码进行文件拷贝,嘿嘿,当然三行中不包含文件是否存在的判断和异常的处理了,只是想说明一下,采用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);
//上面没有进行文件是否存在的判断和异常的处理
}
}
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐