📜  Java I/O-BufferedOutputStream类

📅  最后修改于: 2020-09-27 02:01:53             🧑  作者: Mango

Java BufferedOutputStream类

Java BufferedOutputStream类用于缓冲输出流。它在内部使用缓冲区存储数据。与将数据直接写入流相比,它提高了效率。因此,它可以提高性能。

要在OutputStream中添加缓冲区,请使用BufferedOutputStream类。让我们看看在OutputStream中添加缓冲区的语法:

OutputStream os= new BufferedOutputStream(new FileOutputStream("D:\\IO Package\\testout.txt"));

Java BufferedOutputStream类声明

让我们看一下Java.io.BufferedOutputStream类的声明:

public class BufferedOutputStream extends FilterOutputStream

Java BufferedOutputStream类构造函数

Constructor Description
BufferedOutputStream(OutputStream os) It creates the new buffered output stream which is used for writing the data to the specified output stream.
BufferedOutputStream(OutputStream os, int size) It creates the new buffered output stream which is used for writing the data to the specified output stream with a specified buffer size.

Java BufferedOutputStream类方法

Method Description
void write(int b) It writes the specified byte to the buffered output stream.
void write(byte[] b, int off, int len) It write the bytes from the specified byte-input stream into a specified byte array, starting with the given offset
void flush() It flushes the buffered output stream.

BufferedOutputStream类的示例:

在此示例中,我们将文本信息写入连接到FileOutputStream对象的BufferedOutputStream对象中。 flush()刷新一个流的数据并将其发送到另一流。如果您已将一个流与另一个流连接,则需要这样做。

package com.javatpoint;
import java.io.*;
public class BufferedOutputStreamExample{  
public static void main(String args[])throws Exception{  
 FileOutputStream fout=new FileOutputStream("D:\\testout.txt");  
 BufferedOutputStream bout=new BufferedOutputStream(fout);  
 String s="Welcome to javaTpoint.";  
 byte b[]=s.getBytes();  
 bout.write(b);  
 bout.flush();  
 bout.close();  
 fout.close();  
 System.out.println("success");  
}  
}

输出:

Success

testout.txt

Welcome to javaTpoint.