java基础(13)缓冲流,对象流,转换流

1.缓冲流
提高执行效率,,让操作更加灵活
1.字节缓冲输入流BufferedInputStream的使用

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
//读取一个文件
public class Test {
   
	public static void main(String[] args) throws IOException {
   
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("source.txt"));		
		byte[] bs = new byte[30];
		int num = 0;
		while((num = bis.read(bs))!=-1) {
   
			String str = new String(bs,0,num);
			System.out.println(str);
		}
		if (bis!=null) {
   
			bis.close();
		}
	}
}

在这里插入图片描述

2.BufferedOutputStream缓冲字符输出流的使用

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test2 {
   
	public static void main(String[] args) throws IOException {
   
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("b.txt"));
		byte[] bs = {
   97,98,99,100,101,102};
		bos.write(bs,0,bs.length);
		bos.flush();
		if (bos!=null) {
   
			bos.close();
		}
	}
}

在这里插入图片描述

3.BufferedReader字节输入流的使用
readLine() 读取一行数据

package com.zx.demo2;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test3 {
   
	public static void main(String[] args) throws IOException {
   		
		BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
		// 正常读取方式
		char[] cs = new char[10];
		int len = 0;
		while((len = reader
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值