zl程序教程

您现在的位置是:首页 >  其他

当前栏目

使用NIO进行文件拷贝详解编程语言

文件nio编程语言 使用 详解 进行 拷贝
2023-06-13 09:20:29 时间

只要三行代码进行文件拷贝,嘿嘿,当然三行中不包含文件是否存在的判断和异常的处理了,只是想说明一下,采用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); 

 //上面没有进行文件是否存在的判断和异常的处理 

}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10525.html

cjava