📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:37.887000             🧑  作者: Mango

Java Java类

Java Java类

此类实现了一个输出流过滤器,用于以“deflate”压缩格式压缩数据。它还用作其他类型的压缩过滤器的基础,例如 GZIPOutputStream。
构造函数和描述

  • DeflaterOutputStream(OutputStream out) :使用默认压缩器和缓冲区大小创建一个新的输出流。
  • DeflaterOutputStream(OutputStream out, boolean syncFlush) :使用默认压缩器、默认缓冲区大小和指定的刷新模式创建新的输出流。
  • DeflaterOutputStream(OutputStream out, Deflater def) :使用指定的压缩器和默认缓冲区大小创建一个新的输出流。
  • DeflaterOutputStream(OutputStream out, Deflater def, boolean syncFlush) :使用指定的压缩器、刷新模式和默认缓冲区大小创建一个新的输出流。
  • DeflaterOutputStream(OutputStream out, Deflater def, int size) :使用指定的压缩器和缓冲区大小创建一个新的输出流。
  • DeflaterOutputStream(OutputStream out, Deflater def, int size, boolean syncFlush) :使用指定的压缩器、缓冲区大小和刷新模式创建一个新的输出流。

方法:

  • void close() :将剩余的压缩数据写入输出流并关闭底层流。
    Syntax :public void close()
               throws IOException
    Overrides:
    close in class FilterOutputStream
    Throws:
    IOException
  • protected void deflate() :将下一个压缩数据块写入输出流。
    Syntax :protected void deflate()
                    throws IOException
    Throws:
    IOException
  • void finish() :在不关闭底层流的情况下完成将压缩数据写入输出流。
    Syntax :public void finish()
                throws IOException
    Throws:
    IOException
  • void flush() :刷新压缩的输出流。
    Syntax :public void flush()
               throws IOException
    Overrides:
    flush in class FilterOutputStream
    Throws:
    IOException
  • void write(byte[] b, int off, int len) :将字节数组写入压缩输出流。
    Syntax :public void write(byte[] b,
             int off,
             int len)
               throws IOException
    Overrides:
    write in class FilterOutputStream
    Parameters:
    b - the data to be written
    off - the start offset of the data
    len - the length of the data
    Throws:
    IOException
  • void write(int b) :将一个字节写入压缩输出流。
    Syntax :public void write(int b)
               throws IOException
    Overrides:
    write in class FilterOutputStream
    Parameters:
    b - the byte to be written
    Throws:
    IOException
//Java program to demonstrate DeflaterOutputStream
  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
  
class DeflaterOutputStreamDemo
{
    public static void main(String[] args) throws IOException 
    {
  
        FileOutputStream fos = new FileOutputStream("file2.txt");
  
        //Assign FileOutputStream to DeflaterOutputStream
        DeflaterOutputStream dos = new DeflaterOutputStream(fos);
  
        //write it into DeflaterOutputStream
        for (int i = 0; i <10 ; i++) 
        {
            dos.write(i);
        }
          
        //illustrating flush() method()
        dos.flush();
          
        //illustrating finish()
        //Finishes writing compressed data to the output stream
        // without closing the underlying stream
        dos.finish();
          
        //fos is not closed
        //writing some data on file
        fos.write('G');
      
        //Writes remaining compressed data to the output stream
        // closes the underlying stream.
        dos.close();
    }
}

注意:在线 IDE 上看不到程序的输出,因为此处无法读取 file2.txt。