该类实现了一个输出流过滤器,用于压缩“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):用指定的压缩器,缓冲区大小和flush模式创建一个新的输出流。
方法:
void close():将剩余的压缩数据写入输出流并关闭底层流。
语法: public void close()
throws IOException
覆盖:
FilterOutputStream
抛出:
IOException
protected void deflate():将下一个压缩数据块写入输出流。
语法: protected void deflate()
throws IOException
抛出:
IOException
void finish():完成将压缩数据写入输出流而不关闭基础流。
语法: public void finish()
throws IOException
抛出:
IOException
void flush():刷新压缩的输出流。
语法: public void flush()
throws IOException
覆盖:
FilterOutputStream
抛出:
IOException
void write(byte [] b,int off,int len):将一个字节数组写入压缩输出流。
语法: public void write(byte [] b,
int off,
int len)
throws IOException
覆盖:
在类FilterOutputStream中写入
参数:
b - 要写入的数据
off - 数据的起始偏移量
len - 数据的长度
抛出:
IOException
void write(int b):将一个字节写入压缩的输出流。
语法: public void write(int b)
throws IOException
覆盖:
在类FilterOutputStream中写入
参数:
b - 要写入的字节
抛出:
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();
}
}