使用NIO进行文件拷贝,nio文件拷贝,只要三行代码进行文件拷贝
分享于 点击 5288 次 点评:92
使用NIO进行文件拷贝,nio文件拷贝,只要三行代码进行文件拷贝
只要三行代码进行文件拷贝,嘿嘿,当然三行中不包含文件是否存在的判断和异常的处理了,只是想说明一下,采用FileChannel的API的方便性。
package com.ejintai.test;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); //上面没有进行文件是否存在的判断和异常的处理 }}//该片段来自于http://byrx.net
用户点评