java缓冲输入流_Java缓冲输入输出流

Java缓冲输入输出流

缓冲字节流

默认有8192字节的缓冲区

8cb79d513571e46f6ef1438311004dfe.png

package com.wkcto.chapter06.filterstream;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

/**

* 字节缓冲流

* BufferedInputStream/BufferedOutputStream

* @author 蛙课网

*

*/

public class Test01 {

public static void main(String[] args) throws IOException {

//1)使用字节缓冲流读取文件内容

//readData();

//2)使用字节缓冲流保存数据到文件

writeData();

}

//使用字节缓冲流保存数据到文件

private static void writeData() throws IOException {

//在当前程序与指定的文件间建立字节流通道

OutputStream out = new FileOutputStream("d:/def.txt");

//使用字节缓冲流对out字节流进行包装(缓冲)

BufferedOutputStream bos = new BufferedOutputStream(out);

//使用缓冲流保存数据, 现在是把数据保存到缓冲流的缓冲区中

bos.write(97);

byte[] bytes = "wkcto is a good websit".getBytes();

bos.write(bytes);

//bos.flush(); //把缓冲区的数据清空到文件里

bos.close();

}

//使用字节缓冲流读取文件内容

private static void readData() throws IOException {

//在当前程序与指定的文件之间建立字节 流通道

InputStream in = new FileInputStream("d:/abc.txt");

//对字节流进行缓冲

BufferedInputStream bis = new BufferedInputStream(in);

//使用缓冲字节流读取文件内容

int cc = bis.read();

while( cc != -1){

System.out.print( (char)cc );

cc = bis.read();

}

bis.close(); //关闭缓冲流, 也会把被包装的字节流关闭

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值