字符流和字节流的桥梁InputStreamReader

InputStreamReader(继承Reader)
简介: 将字节流转换为字符流, 字节流通向字符流的桥梁,如果不指定字符集编码,则解码过程
将使用平台默认的字符编码,如:UTF-8
文本文件存储是A编码,然后如果以B编码进⾏读取则会乱码

//使用系统默认编码集
public InputStreamReader(InputStream in)
//指定指定编码集创建对象
public InputStreamReader(InputStream in, String charsetName)

常用API
int read()
讲解:读取单个字符
int read(char[] cbuf, int off, int len)
讲解:读取一部分字符到数组里面,从数组下标off处放置length长度的字符
int read(char []cbuf)
讲解:将读取到的字符存到数组中,返回读取的字符数
void close()
讲解:关闭流释放资源

代码:

public static void main(String[] args)throws Exception{
        test1("C:\\Users\\Administrator\\Desktop\\test\\9.txt");
    }

    public static void test1(String path)throws Exception{
        InputStream in=new FileInputStream(path);
        InputStreamReader ins=new InputStreamReader(in,"GBK");
        BufferedReader reader=new BufferedReader(ins);
		//数组输出
//        char[] chars=new char[1024];
//        int size;
//        while ((size=reader.read(chars,0,chars.length))!=-1){
//            System.out.println(new String(chars,0,size));
//        }
		
		//行输出
        String line;
        while ((line=reader.readLine())!=null){
            System.out.println(line);
        }
        reader.close();
    }

效果截图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值