跟我学IO(BufferedOutputStream类)

java的BufferedOutputStream类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。该类是IO类中高级类。

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();
				}
			}
		}
	}
	
}
人永远是要学习的。死的时候,才是毕业的时候。 —— 萧楚女
0 不喜欢
说说我的看法 -
全部评论(
没有评论
目录
热门标签
热门文章
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号