缓冲流实现文件的copy,可以对图片,视频,zip,rar等文件进行操作
package com.io.buffered; import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import org.junit.test; /** * 使用缓冲流实现文件的copy */ public class bufferedstreamfile { // 非文本文件放copy @test public void copyfiletest() { // 记录下时间 long start = system.currenttimemillis(); string src = "c:\\users\\administrator\\desktop\\1.png"; string dest = "c:\\users\\administrator\\desktop\\2.png"; copyfile(src, dest); // 结束时间 long end = system.currenttimemillis(); system.out.println("耗时:" + (end - start)); } public static void copyfile(string src, string dest) { // 3、创bufferedinputstream 包装对应的节点流,用于提高效率 bufferedinputstream bis = null; // 4、创建bufferedoutputstream 包装对应的节点流,用于提高效率 bufferedoutputstream bos = null; try { // 1、创建fileinputstream fileinputstream fis = new fileinputstream(src); // 2、创建fileoutputstream fileoutputstream fos = new fileoutputstream(dest); bis = new bufferedinputstream(fis); bos = new bufferedoutputstream(fos); // 5、读取对应的文件内容 byte[] b = new byte[1024]; int len = 0; while ((len = bis.read(b)) != -1) { // 6将读取内容写到目标地点 bos.write(b, 0, len); } // bos.flush();// 强制情况缓冲区【防止数据的丢失,一般情况不要用加】 } catch (ioexception e) { e.printstacktrace(); } // 7、关闭流 if (bos != null) { try { bos.close(); } catch (ioexception e) { e.printstacktrace(); } } if (bis != null) { try { bis.close(); } catch (ioexception e) { e.printstacktrace(); } } } }
如对本文有疑问, 点击进行留言回复!!
宝塔工具+javaweb+mysql+tomcat部署项目教程(详细教程适合小白)
荐 SSM Web项目部署(环境配置+打成War包+Tomcat部署)
mac os 新系统配置—— anaconda安装及管理——idea安装配置
ubuntu 16.04 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) 亲试可行
网友评论