zoukankan      html  css  js  c++  java
  • java io

    package cn.itcast_03;
       
      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
       
      /*
      * 数据源:
      * d:\哥有老婆.mp4
      * 目的地:
      * 项目路径下copy.mp4
      *
      * 四种方式:
      * A:基本字节流一次读写一个字节 67023毫秒
      * B:基本字节流一次读写一个字节数组 共耗时:102毫秒
      * C:高效字节流一次读写一个字节 共耗时:650毫秒
      * D:高效字节流一次读写一个字节数组 共耗时:36毫秒
      */
      public class CopyMP4Demo {
      public static void main(String[] args) throws IOException {
      long start = System.currentTimeMillis();
      // method1();
      // method2();
      // method3();
      method4();
      long end = System.currentTimeMillis();
      System.out.println("共耗时:" + (end - start) + "毫秒");
      }
       
      // 基本字节流一次读写一个字节
      public static void method1() throws IOException {
      FileInputStream fis = new FileInputStream("d:\哥有老婆.mp4");
      FileOutputStream fos = new FileOutputStream("copy1.mp4");
       
      int by = 0;
      while ((by = fis.read()) != -1) {
      fos.write(by);
      }
       
      fos.close();
      fis.close();
      }
       
      // 基本字节流一次读写一个字节数组
      public static void method2() throws IOException {
      FileInputStream fis = new FileInputStream("d:\哥有老婆.mp4");
      FileOutputStream fos = new FileOutputStream("copy2.mp4");
       
      byte[] bys = new byte[1024];
      int len = 0;
      while ((len = fis.read(bys)) != -1) {
      fos.write(bys, 0, len);
      }
       
      fos.close();
      fis.close();
      }
       
      // 高效字节流一次读写一个字节
      public static void method3() throws IOException {
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
      "d:\哥有老婆.mp4"));
      BufferedOutputStream bos = new BufferedOutputStream(
      new FileOutputStream("copy3.mp4"));
       
      int by = 0;
      while ((by = bis.read()) != -1) {
      bos.write(by);
      }
       
      bos.close();
      bis.close();
      }
       
      // 高效字节流一次读写一个字节数组
      public static void method4() throws IOException {
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
      "d:\哥有老婆.mp4"));
      BufferedOutputStream bos = new BufferedOutputStream(
      new FileOutputStream("copy4.mp4"));
       
      byte[] bys = new byte[1024];
      int len = 0;
      while ((len = bis.read(bys)) != -1) {
      bos.write(bys, 0, len);
      }
       
      bos.close();
      bis.close();
      }
      }
    1:字节缓冲流(掌握)
      (1)为了提高效率,java就提供了字节缓冲区流。
      (2)字节缓冲区流
      BufferedInputStream
      BufferedOutputStream
      (3)复制视频文件
      A:基本字节流一次读写一个字节
      B:基本字节流一次读写一个字节数组
      C:高效字节流一次读写一个字节
      D:高效字节流一次读写一个字节数组
      (4)流的一些名字
      A:节点流 基本流
      B:处理流 高级流
       
      2:转换流(理解)
      (1)因为字节流操作文本文件不是特别的方便,所以,java就提供了转换流。
      用来让我们操作文本文件数据更方便一些
      (2)转换流本身是一个字符流
      字符流 = 字节流 + 编码表
      (3)编码表
      字符和对应数据组成的一张表
       
      常见编码表:
      ASCII
      ISO-8859-1
      GB2312,GBK,GB18030
      BIG5
      UTF-8
      (4)编码问题
      A:String的编码问题
      B:IO流中的编码问题
       
      方案:
      统一编码,就不会有任何问题。
       
      3:字符流(掌握)
      (1)字符流体系
      Reader
      |--InputStreamReader
      |--FileReader
      |--BufferedReader
      Writer
      |--OutputStreamWriter
      |--FileWriter
      |--BufferedWriter
      (2)字符流操作案例
      A:基本字符流一次读写一个字符
      B:基本字符流一次读写一个字符数组
      C:高效字符流一次读写一个字符
      D:高效字符流一次读写一个字符数组
      E:高效字符流一次读写一个字符串
       
      4:IO流小结(掌握)
      IO流
      |--字节流
      |--输入流 InputStream
      |--FileInputStream
      |--BufferedInputStream
      |--输出流 OutputStream
      |--FileOutputStream
      |--BufferedOutputStream
      |--字符流
      |--输入流 Reader
      |--FileReader
      |--BufferedReader
      |--输出流 Writer
      |--FileWriter
      |--BufferedWriter
       
      复制文本文件:
      9种方案。
      字节流:4种
      字符流:5种
      建议使用字符流。并且用最后一种。
       
      复制二进制流数据:
      4种方案。
       
      如果你不知道,用字节流
  • 相关阅读:
    protege5.2基础教程
    Controller返回json的编码处理
    QQ开放平台网页应用接口测试
    前端开发工程师
    前端开发工程师
    前端开发工程师
    UniMelb Comp30022 IT Project (Capstone)
    Java开发工程师(Web方向)
    Java开发工程师(Web方向)
    Java开发工程师(Web方向)
  • 原文地址:https://www.cnblogs.com/cheyunhua/p/10911539.html
Copyright © 2011-2022 走看看