zoukankan      html  css  js  c++  java
  • OutputStream-InputStream-FileOutputStream-FileInputStream-BufferedOutputStream-BufferedInputStream-四种复制方式-单层文件夹复制

      字节流两套:
             java.lang.Object--java.io.OutputStream--java.io.FileOutputStream
             java.lang.Object--java.io.InputStream--java.io.FileInputStream
    ==================================================================
    一.OutputStream 抽象类(java.io) 字节输出流
         概述:OutputStream 类就是专门用于从程序中将数据写入到介质中; 
         定义:public abstract class OutputStream extends Object implements Closeable, Flushable
             OutputStream 类:此抽象类是表示输出字节流的所有类的超类。
         Direct Known Subclasses:FileOutputStream,PipedOutputStream
         注意:io流不能操作文件夹(文件夹中当然不能直接写内容!!!)
                 操作的最小单位是bit(位)
                 存储的最小单位是byte(字节)
                 1byte = 8 bit
         构造方法:OutputStream() 
         常用方法:
             public void close() throws IOException{}: 关闭流
             public void flush() throws IOException{}: 刷新流
             public void write(byte[] b) throws IOException{}:-->输出流对象
             public void write(byte[] b, int off, int len) throws IOException{}:-->输出流对象
         抽象方法: public abstract write(int b):(抽象方法) -->输出流对象

    二.FileOutputStream 类(java.io)字节输出流的子类
         概述:OutputStream 常用子类;  
         构造方法:该类使用构造方法时,可直接创建文件,若文件存在则直接覆盖,想要不被覆盖,构造时传入参数(true);
             FileOutputStream(String name) throws FileNotFoundException:创建一个向具有指定名称的文件中写入数据的输出文件流。
                                         Name 表示一个文件的路径,可以是相对的也可以是绝对的!
             FileOutputStream(File file) throws FileNotFoundException:创建一个向指定 File 对象表示的文件中写入数据的文件输出流.
             续写:
                 FileOutputStream(File file, boolean append) throws FileNotFoundException:创建一个向指定 File 对象表示的文件中写入数据的文件输出流
                                                             需要传递append的值为:true即可,则不覆盖已存在文件,而是在文件后续写;
                 FileOutputStream(String name, boolean append)throws FileNotFoundException:创建一个向具有指定 name 的文件中写入数据的输出文件流。
             换行:
                 使用字符流的时候,可以直接写入:“ ”;
                 使用字节流的时候,需要写入“ ”的字节数组;
         常用方法:
             public void write(byte[] b) throws IOException{}: b.length 个字节从指定 byte 数组写入此文件输出流中。
             public void write(byte[] b, int off, int len) throws IOException{}:将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
             public void write (int b) throws IOException{}:将指定字节个数写入此文件输出流.
             public void close() throws IOException{}:关闭此文件输出流并释放与此流有关的所有系统资源。

            继承OutputStream抽象类的flush()方法

            String 类的一个常用方法:
                 public byte[] getBytes(){}:使用平台的默认字符集将此 String 拆分为 byte 序列,并将结果存储到一个新的 byte 数组中。
    ======================================================================
             java.lang.Object--java.io.InputStream--java.io.FileInputStream
    ======================================================================
    三.InputStream 抽象类(java.io) 字节输入流
         定义:public abstract class InputStream extends Object implements Closeable
         概述:InputStream 此抽象类是表示字节输入流的所有类的超类。
         Direct Known Subclasses:FileInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream
         抽象方法:
             public abstract int read() throws IOException:
         常用方法:
             public int read(byte[] b)  throws IOException{}:读取字节数组;
             public int read(byte[] b, int off, int len) throws IOException{}:读取部分字节数组;
             public void close() throws IOException{}:关闭流;


    四.FileInputStream 类(java.io) 字节输入流的子类
         概述:InputStream 类的常用子类;FileInputStream 类是专门用于从文件中读取字节数据的流,读取到的内容以十进制的数据的形式返回;
             如果想要将十进制的数据转换成对应的字符,需要程序员对照码表转换;
             具体转换方式有2种:
                 1:直接强转成char类型的字符;(适用于一个数字)
                 2:使用String类的构造方法;(适用于一个或多个数字)
         构造方法:
             FileInputStream(String name) throws FileNotFoundException:通过打开一个到实际文件的连接来创建一个 FileInputStream,
                                         该文件通过文件系统中的路径名 name 指定。
             FileInputStream(File file) throws FileNotFoundException:通过打开一个到实际文件的连接来创建一个 FileInputStream,
                                         该文件通过文件系统中的 File 对象 file 指定。
         常用方法:
             public int read() throws IOException {}:从此输入流中读取一个数据字节。返回的是读取到的这个字节的码值!
             public int read(byte[] b) throws IOException {}:从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。返回的是读取到的字节的个数!
             public int read(byte[] b, int off, int len) throws IOException {}:从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。返回的是读取到的字节的个数!
             public void close() throws IOException{}:关闭此文件输入流并释放与此流有关的所有系统资源。
    ====================================================================
         高效字节流两套:
             java.lang.Object--java.io.OutputStream--java.io.FilterOutputStream--java.io.BufferedOutputStream
             java.lang.Object--java.io.InputStream--java.io.FilterInputStream--java.io.BufferedInputStream
    ==================================================================
    五.BufferedOutputStream 类(java.io)
         定义:public class BufferedOutputStream extends FilterOutputStream
             属于OutputStream的孙子类
         构造方法:BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
         常用方法:
             public void flush() throws IOException{}:
             public void write(byte[] b, int off, int len) throws IOException{}:
             public void write(int b) throws IOException{}:

            继承自FilterOutputStream的close()方法;

    六.BufferedInputStream 类(java.io)
         定义:public class BufferedInputStream extends FilterInputStream
         构造方法:BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,即输入流in,以便将来使用。
         常用方法:
             public void close() throws IOException{}:关闭流;
             public int read() throws IOException{}:读取;
             public int read(byte[] b, int off, int len) throws IOException{}:以字节数组形式读取;

    =============================================================
    七.四种方式复制文件:
    1:高效字节流加数组

    复制代码
      1 //1:高效流加数组,复制"E:\JAVAPractice\IO\1.jpg"
      2  import java.io.BufferedInputStream;
      3  import java.io.FileInputStream;
      4  import java.io.BufferedOutputStream;
      5  import java.io.FileOutputStream;
      6  import java.io.IOException;
      7  public class Copy01{
      8      public static void main(String[] args) throws IOException{
      9          //抓取开始时间
     10         long t1 = System.currentTimeMillis();
     11          //创建高效流
     12         BufferedInputStream bin = new BufferedInputStream(new FileInputStream("E:\JAVAPractice\IO\1.jpg"));
     13          BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("E:\JAVAPractice\IO\1_1.jpg"));
     14          //定义字节数组存储读取内容
     15         byte[] b = new byte[1024];
     16          int i = -1;//读取到的字节个数
     17         while((i = bin.read(b)) != -1){
     18              bout.write(b,0,i);
     19              bout.flush();
     20          }
     21          bout.close();
     22          bin.close();
     23          //抓取程序结束时间
     24         long t2 = System.currentTimeMillis();
     25         System.out.println("耗时:"+(t2-t1)/1000.0);//耗时:0.25
     26      }
     27  }
    复制代码

    2:基本字节流加数组

    复制代码
      1 //2:基本流加数组,复制"E:\JAVAPractice\IO\1.jpg
      2  import java.io.FileOutputStream;
      3  import java.io.FileInputStream;
      4  import java.io.IOException;
      5  public class Copy02{
      6      public static void main(String[] args) throws IOException{
      7          //记录程序开始时间
      8         long t1 = System.currentTimeMillis();
      9          //创建基本字节流对象
     10         FileInputStream in = new FileInputStream("E:\JAVAPractice\IO\1.jpg");
     11          FileOutputStream out = new FileOutputStream("E:\JAVAPractice\IO\1_2.jpg");
     12          //定义数组存储读取到的数据
     13         byte[] b = new byte[1024];
     14          int i = -1;//读取到的字节个数
     15         while((i = in.read(b)) != -1){
     16              out.write(b,0,i);
     17              out.flush();
     18          }
     19          //关闭流
     20         in.close();
     21          out.close();
     22          //记录程序结束时间
     23         long t2 = System.currentTimeMillis();
     24          System.out.println("耗时:"+(t2-t1)/1000.0);//耗时:0.344
     25      }
     26  }
    复制代码

    3:高效字节流逐字节

    复制代码
      1 //3:高效流逐字节,复制"E:\JAVAPractice\IO\1.jpg
      2  import java.io.BufferedInputStream;
      3  import java.io.FileInputStream;
      4  import java.io.BufferedOutputStream;
      5  import java.io.FileOutputStream;
      6  import java.io.IOException;
      7  public class Copy03{
      8      public static void main(String[] args) throws IOException{
      9          //记录系统时间
     10         long t1 = System.currentTimeMillis();
     11          //创建高效字符流
     12         BufferedInputStream bin = new BufferedInputStream(new FileInputStream("E:\JAVAPractice\IO\1.jpg"));
     13          BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("E:\JAVAPractice\IO\1_3.jpg"));
     14          //定义i为ASCII码值
     15         int i = -1;
     16          while((i = bin.read()) != -1){
     17              bout.write(i);
     18          }
     19          //关闭流
     20         bin.close();
     21          bout.close();
     22          //记录系统时间
     23         long t2 = System.currentTimeMillis();
     24          System.out.println("耗时:"+(t2-t1)/1000.0);//耗时:0.625
     25      }
     26  }
    复制代码

    4:基本字节流逐字节复制

    复制代码
      1 //4:基本流逐字节复制,E:\JAVAPractice\IO\1.jpg
      2  import java.io.FileInputStream;
      3  import java.io.FileOutputStream;
      4  import java.io.IOException;
      5  public class Copy04{
      6      public  static void main(String[] args) throws IOException{
      7          //记录系统时间
      8         long t1 = System.currentTimeMillis();
      9          //创建基本字节流对象
     10         FileInputStream in = new FileInputStream("E:\JAVAPractice\IO\1.jpg");
     11          FileOutputStream out = new FileOutputStream("E:\JAVAPractice\IO\1_4.jpg");
     12          //定义不在ASCII码值范围内的i
     13          int i = -1;
     14          while((i = in.read()) != -1){
     15              out.write(i);
     16              out.flush();
     17          }
     18          //关闭流
     19         out.close();
     20          in.close();
     21          //记录系统时间
     22         long t2 = System.currentTimeMillis();
     23          System.out.println("耗时:"+(t2-t1)/1000.0);//耗时:52.383
     24      }
     25  }
    复制代码


    ===============================
    八.单层文件夹复制

    复制代码
      1 //复制E:\JAVAPractice\IO\demo该单层文件夹下所有txt格式文件
      2 import java.io.File;
      3  import java.io.IOException;
      4  import java.io.FileInputStream;
      5  import java.io.FileOutputStream;
      6  public class DirCopy{
      7      public static void main(String[] args) throws IOException{
      8          //创建源文件夹对象
      9         File src = new File("E:\JAVAPractice\IO\demo");
     10          //创建目标文件夹对象
     11         File dest = new File("E:\JAVAPractice\IO\democopy");
     12          dest.mkdir();
     13          //获取源文件夹中所有对象
     14         File[] files = src.listFiles();
     15          //迭代
     16         for(File f : files){
     17             //将所有文件包装成输入流对象
     18             FileInputStream in = new FileInputStream(f);
     19              //i表示读取的字节数
     20             int i = -1;
     21              byte[] b = new byte[1024];
     22              //准备输出流对象
     23             File fileOut = new File(dest,f.getName());
     24              FileOutputStream out = new FileOutputStream(fileOut);
     25              while((i = in.read(b)) != -1){
     26                  out.write(b,0,i);
     27                  out.flush();
     28              }
     29              out.close();//out在foreach内定义,所以在foreach内关闭
     30             in.close();//同上
     31         }
     32          System.out.println("操作完成");
     33      }
     34  }
    复制代码

    多层文件夹复制--递归调用???

  • 相关阅读:
    c语言练习17——输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
    c语言练习16——输入两个正整数m和n,求其最大公约数和最小公倍数
    c语言练习15——条件运算符的嵌套
    c语言练习14——将一个正整数分解质因数
    CentOS下Cassandra集群搭建
    一台linux服务器挂载另外一台linux服务器文件系统
    zabbix分布式监控多网段的部署与实现
    CentOS安装MySQL详解
    vcenter 7.0 安装 vRealize Operations Manager
    Zabbix分布式部署详细
  • 原文地址:https://www.cnblogs.com/firstdream/p/8502533.html
Copyright © 2011-2022 走看看