zoukankan      html  css  js  c++  java
  • Java使用基本字节流OutputStream的四种方式对于数据复制(文本,音视频,图像等数据)

      1 //package 字符缓冲流bufferreaderDemo;
      2 
      3 import java.io.BufferedOutputStream;
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.io.BufferedInputStream;
      7 import java.io.IOException;
      8 
      9 /*
     10  * 四种方式实现大文件数据的读取写入--->复制
     11  * 1.基本字节流一次读取一个字节        最慢
     12  * 2.基本字节流一次读取一个字节数组     较快  (优选)
     13  * 3.高效字节流一次读取一个字节        比较快
     14  * 4.高校字节流一次读取一个字节数组     很快   (优选) 
     15  * 
     16  */
     17 public class Test2 {
     18     public static void main(String[] args) throws IOException {
     19         long start = System.currentTimeMillis();
     20 
     21         // method1("E:\b.txt", "c.txt");
     22         // method2("E:\b.txt", "c.txt");
     23         // method3("E:\b.txt", "c.txt");
     24         method4("E:\b.txt", "c.txt");
     25 
     26         long end = System.currentTimeMillis();
     27         System.out.println("共耗时: " + (end - start) + "毫秒");
     28     }
     29 
     30     // 1.基本方法字节流一次读取一个字节
     31     public static void method1(String srcPath, String destPath)
     32             throws IOException {
     33         // 读取数据对象
     34         FileInputStream fis = new FileInputStream(srcPath);
     35         // 写入数据目标文件路径
     36         FileOutputStream fos = new FileOutputStream(destPath);
     37         // 数据读写
     38         // 直接以单字节读取
     39         int by = 0;
     40         while ((by = fis.read()) != -1) {
     41             fos.write(by);
     42         }
     43         // 关闭流
     44         fos.close();
     45         fis.close();
     46     }
     47 
     48     // 2.基本字节读取一次读取一个数组
     49     public static void method2(String srcPath, String destPath)
     50             throws IOException {
     51         // 数据读取的对象封装
     52         FileInputStream fis = new FileInputStream(srcPath);
     53         // 数据写入对象封装
     54         FileOutputStream fos = new FileOutputStream(destPath);
     55         // 数据的读写
     56         byte[] bys = new byte[1024];
     57         int len = 0;
     58         while ((len = fis.read(bys)) != -1) {
     59             fos.write(bys, 0, len);
     60         }
     61         // 关闭流
     62         fis.close();
     63         fos.close();
     64     }
     65 
     66     // 3.高效字节流一次读取一个字节
     67     public static void method3(String srcPath, String destPath)
     68             throws IOException {
     69         // 数据读取对象封装
     70         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
     71                 srcPath));
     72         // 数据写入对象封装
     73         BufferedOutputStream bos = new BufferedOutputStream(
     74                 new FileOutputStream(destPath));
     75 
     76         // 数据读写操作
     77         int by = 0;
     78         while ((by = bis.read()) != -1) {
     79             bos.write(by);
     80         }
     81 
     82         // 关闭流
     83         bos.close();
     84         bis.close();
     85     }
     86 
     87     // 4.高效字节流读取一个字节数组
     88     public static void method4(String srcPath, String destPath)
     89             throws IOException {
     90         // 数据读取对象封装
     91         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
     92                 srcPath));
     93         // 数据写入对象
     94         BufferedOutputStream bos = new BufferedOutputStream(
     95                 new FileOutputStream(destPath));
     96 
     97         // 数据读写操作
     98         byte[] bys = new byte[1024];
     99         int len = 0;
    100         while ((len = bis.read(bys)) != -1) {
    101             bos.write(bys, 0, len);
    102         }
    103 
    104         // 关闭流
    105         bos.close();
    106         bis.close();
    107     }
    108 }
  • 相关阅读:
    LNMP分离部署
    PXE
    Mysql
    07.23 课堂随笔 学习了相关的标签
    [Prodinner项目]学习分享_第三部分_Service层(业务逻辑层)
    Beginning Windows Azure Development Guide
    5分钟浅析简单工厂模式
    [Prodinner项目]学习分享_第一部分_Model层
    [Prodinner项目]学习分享_第四部分(完结篇)_Controller层(控制器)
    C# Windows service 开发笔录
  • 原文地址:https://www.cnblogs.com/fuck1/p/5322317.html
Copyright © 2011-2022 走看看