java的BufferedOutputStream类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。该类是IO类中高级类。
实例:将指定的消息使用BufferedOutputStream类写出到磁盘文件中。
package io.outputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * 将消息输出到output.txt文件 */ public class BufferedOutputStreamTest { public static void main(String[] args) { BufferedOutputStream output = null; try { String msg = "hi!FileOutputStream class."; // 创建文件输出流对象,如果不存在output.txt文件,则创建该文件 output = new BufferedOutputStream( new FileOutputStream(new File("document/output.txt")) ); // 将消息写入到输出流 output.write(msg.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭输出流 if ( null != output ) { try { output.close(); output = null; } catch (IOException e) { e.printStackTrace(); } } } } }