字节流文件复制方式:四种
发布时间:2021-05-20 09:45:01 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* ?* 文件复制方式(字节流),一共4个方式 ?* 1.字节流
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* ?* 文件复制方式(字节流),一共4个方式 ?* 1.字节流读写单个字节?? ??? ?125250ms ?* 2.字节流读写字节数组?? ??? ?193ms ?* 3.字节流缓冲区流读写单个字节?? ?1210ms ?* 4.字节流缓冲区流读写字节数组?? ?73ms ?*/ public class Copy_3 { ?? ?public static void main(String[] args) throws IOException { ?? ??? ?long s=System.currentTimeMillis(); ?? ??? ?copy_4(new File("d:NavicatforMysqL.zip"),new File("d:demoNavicatforMysqL.zip")); ?? ??? ?long e=System.currentTimeMillis(); ?? ??? ?System.out.println(e-s); ?? ?} ?? ? ?? ?//1.字节流读写单个字节 ?? ?public static void copy_1(File src,File desc) throws IOException{ ?? ??? ?FileInputStream fis=new FileInputStream(src); ?? ??? ?FileOutputStream fos=new FileOutputStream(desc); ?? ??? ?int len=0; ?? ??? ?while((len=fis.read())!=-1){ ?? ??? ??? ?fos.write(len); ?? ??? ?} ?? ??? ?fos.close(); ?? ??? ?fis.close(); ?? ?} ?? ? ?? ?//2.字节流读写字节数组 ?? ?public static void copy_2(File src,File desc)throws IOException{ ?? ??? ?FileInputStream fis=new FileInputStream(src); ?? ??? ?FileOutputStream fos=new FileOutputStream(desc); ?? ??? ?int len=0; ?? ??? ?byte[] bytes=new byte[1024]; ?? ??? ?while((len=fis.read(bytes))!=-1){ ?? ??? ??? ?fos.write(bytes,len); ?? ??? ?} ?? ??? ?fos.close(); ?? ??? ?fis.close(); ?? ?} ?? ? ?? ?//3.字节流缓冲区流读写单个字节 ?? ?public static void copy_3(File src,File desc)throws IOException{ ?? ??? ?BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src)); ?? ??? ?BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desc)); ?? ??? ?int len=0; ?? ??? ?while((len=bis.read())!=-1){ ?? ??? ??? ?bos.write(len); ?? ??? ?} ?? ??? ?bos.close(); ?? ??? ?bis.close(); ?? ?} ?? ? ?? ?//4.字节流缓冲区流读写字节数组 ?? ?public static void copy_4(File src,File desc)throws IOException{ ?? ??? ?BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src)); ?? ??? ?BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(desc)); ?? ??? ?int len=0; ?? ??? ?byte[] bytes=new byte[1024]; ?? ??? ?while((len=bis.read(bytes))!=-1){ ?? ??? ??? ?bos.write(bytes,len); ?? ??? ?} ?? ??? ?bos.close(); ?? ??? ?bis.close(); ?? ?} } (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |