zoukankan      html  css  js  c++  java
  • Java学习之IO流(字节流)

    字节流:顾名思义操作(读、写)文件的流对象

    字节流的体系:

    1、InputStream
      |--FileInputStream:将文件数据读取到缓冲区中

    2、OutputStream
      |--FileOutputStream:将缓冲区数据写入文件

    一、FileInputStream和FileOutputStream

     1 // 文件过大,有可能内存溢出
     2     private static void copy_3() throws IOException {
     3     FileInputStream fis = new FileInputStream("源文件路径");
     4     FileOutputStream fos = new FileOutputStream("目标文件路径");
     5     // available()文件大小
     6     byte[] buf = new byte[fis.available()];
     7     fis.read(buf);
     8     fos.write(buf);
     9     fis.close();
    10     fos.close();
    11     }
    12 
    13     // 使用缓冲区对象
    14     private static void copy_2() throws IOException {
    15     FileInputStream fis = new FileInputStream("源文件路径");
    16     BufferedInputStream bfis = new BufferedInputStream(fis);
    17     FileOutputStream fos = new FileOutputStream("目标文件路径");
    18     BufferedOutputStream bfos = new BufferedOutputStream(fos);
    19 
    20     int ch = 0;
    21     while ((ch = bfis.read()) != -1) {
    22         bfos.write(ch);
    23     }
    24     bfis.close();
    25     bfos.close();
    26     }
    27 
    28     // 自定义缓冲区
    29     private static void copy_1() throws IOException {
    30     FileInputStream fis = new FileInputStream("源文件路径");
    31     FileOutputStream fos = new FileOutputStream("目标文件路径");
    32     byte[] buf = new byte[1024];
    33     int len = 0;
    34     while ((len = fis.read(buf)) != -1) {
    35         fos.write(buf, 0, len);
    36     }
    37     fis.close();
    38     fos.close();
    39     }
  • 相关阅读:
    ThinkPHP的ajaxReturn方法的使用
    PHP中如何获取网站根目录物理路径
    MySQL索引覆盖
    php对gzip的使用(实例)
    php对gzip的使用(开启)
    php对gzip的使用(理论)
    ThinkPHP中调用PHPExcel
    PHPExcel正确读取excel表格时间单元格(转载)
    Kubernetes pod网络解析
    vRO 添加已有磁盘到VM
  • 原文地址:https://www.cnblogs.com/WarBlog/p/12132516.html
Copyright © 2011-2022 走看看