zoukankan      html  css  js  c++  java
  • JAVA之用缓冲流读写文件

    public class CopyDemo {
    public static void main(String[] args) throws Exception{
    long time1 = System.currentTimeMillis();
    copy4(new File("d:\ccc.mp4"),new File("e:\ccc.mp4"));
    long time2 = System.currentTimeMillis();
    System.out.println(time2-time1);
    }
    //1.字节流读写单个字节 太慢了
    public static void copy1(File src,File desc) throws Exception{
    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.字节流读写字节数组 550
    public static void copy2(File src,File desc) throws Exception{
    FileInputStream fis = new FileInputStream(src);
    FileOutputStream fos = new FileOutputStream(desc);
    int len = 0;
    byte[] b = new byte[1024*10];
    while ((len=fis.read(b))!=-1){
    fos.write(b,0,len);
    }
    fos.close();
    fis.close();
    }
    //3.字节流缓冲区 读写单个字节 4252
    public static void copy3(File src,File desc) throws Exception{
    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);
    }
    bis.close();
    bos.close();
    }
    //4.字节流缓冲区读写字节数组 585
    public static void copy4(File src,File desc) throws Exception{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
    int len = 0;
    byte[] b = new byte[1024*10];
    while ((len=bis.read(b))!=-1){
    bos.write(b,0,len);
    }
    bos.close();
    bis.close();
    }
  • 相关阅读:
    服务器运维
    mysq配置
    PHP-FPM进程数的设定
    vsftpd 安装配置详细教程
    php-fpm性能优化
    如果不知道MySQL当前使用配置文件(my.cnf)的路径的解决方法
    搭建linux+nginx+mysql+php环境
    PHP 页面编码声明方法详解(header或meta)
    Linux内核的一些知识。
    Connector框架笔记
  • 原文地址:https://www.cnblogs.com/Y-mmeng/p/10606464.html
Copyright © 2011-2022 走看看