OutputStream、InputStream 、FileOutputStream、FileInputStream,字节流API

OutputStream、InputStream是所有字节流的超类,所谓字节流就是一个字节一个字节的读取,类似于c中的二进制读取方法

java.io.OutputStream

Method

  void write(int b)

    Writes the specified byte to this output stream

  void write(byte[] b)    //   String 类的方法,  byte[] b = "中国".getBytes();   

    Writes b.length bytes from the specified byte array to this output stream.

  void write(byte[], int offset, int len)

    Writes len bytes from the specified byte array starting at offset off to this output stream.

        OutputStream out = new FileOutputStream("E:/a.txt");
        out.write(97);   // 写下二进制的97,文本编辑器读出二进制97,查询本机编码表,得出a
        
        byte[] b = {-43,-44};   //
        out.write(b);
        out.close();
View Code

  void flush()

    Flushes this output stream and forces any buffered output bytes to be written out.

  void close()

子类 FileOutputStream

构造函数

  FileOutputStream(File file, boolean append)     //  append 参数不写默认为false,即每次都会创建一个新文件(不管文件是否存在)

    Creates a file output stream to write to the file represented by the specified File object.

  FileOutputStream(String name, boolean append)   // @name  the file path @append 同上

其余方法暂时略,大部分与父类相同

java.io.InputStream

  int read()

    Reads the next byte of data from the input stream

  int read(byte[] b)

    Reads some number of bytes from the input stream and stores them into the buffer array b.

  int read(byte[] b, int off, int len)

    Reads up to len bytes of data from the input stream into an array of bytes.

  

        InputStream in = new FileInputStream("E:/a.txt");  //文件内容 abcde
        //int a = in.read();   // 97 
        byte[] b = new byte[2];
        int a = in.read(b);   // a值 2   b值 ab
        a = in.read(b);   //  a值  2     b值 cd
        a = in.read(b);   // a值 1       b值 ed
        a = in.read(b);   // a值 -1      b值 ed 
        
        // a 为读出的字节数, 读到文件末尾时为 -1
        in.close();
View Code

子类 FileInputStream

构造函数

  FileInputStream(File file, boolean append)

  FileInputStream(String name, boolean append)  // 同上

二进制文件复制

        InputStream in = new FileInputStream("E:/test.avi");
        OutputStream out = new FileOutputStream("E:/copy.avi");
        
        int len;
        byte[] b = new byte[1024];
        while((len = in.read(b)) != -1)
        {
            out.write(b, 0, len);
            // out.flush();
        }
        
        in.close();
        out.close();
View Code

BufferedOutputStream、BufferedInputStream 提高程序读取效率

构造函数 BufferedOutputStream(OutputStream out)

下面是一个对比,复制102M的文件耗时

public static void copy_4(String desPath, String srcPath) throws IOException{
        
         BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcPath));
   
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desPath));
        
        int len;
        byte[] b = new byte[1024];
        while((len = in.read(b)) != -1)
        {
            out.write(b, 0, len);         
        }
        
        in.close();
        out.close();
}

/*
     * 一个字节一个的读,没缓冲     408,891
     * 一个字节一个的读,有缓冲       4979
     * 每次读 1024字节, 没缓冲      898
     * 每次读 1024字节, 有缓冲      236
    */
View Code
原文地址:https://www.cnblogs.com/YKang/p/7283333.html