您好,欢迎访问一九零五行业门户网

使用Java NIO非直接缓冲区进行文件复制

通过java nio 非直接缓冲区拷贝文件
/** * 通过java nio 非直接缓冲区拷贝文件 * * @param sourcepath 源文件路径 * @param targetpath 目标文件路径 */ public static void copyfilebychannel(string sourcepath, string targetpath) { filechannel outchannel = null; filechannel inchannel = null; fileinputstream fis = null; fileoutputstream fos = null; try { fis = new fileinputstream(sourcepath); fos = new fileoutputstream(targetpath); //获取通道 inchannel = fis.getchannel(); outchannel = fos.getchannel(); //分配指定大小的缓冲区 bytebuffer buf = bytebuffer.allocate(1024); while (inchannel.read(buf) != -1) { //转换为读取数据模式 buf.flip(); //写入到磁盘 outchannel.write(buf); //清空缓冲区 buf.clear(); } } catch (exception e) { e.printstacktrace(); } finally { //关闭流 try { if (outchannel != null) { outchannel.close(); } if (inchannel != null) { inchannel.close(); } if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (ioexception e) { e.printstacktrace(); } } }
以上就是使用java nio非直接缓冲区进行文件复制的详细内容。
其它类似信息

推荐信息