Java学习笔记——IO流

今天在学习Java IO流的时候遇到了一个问题,记录一下

  1. 我创建了一个文本文件,内容为“我” ,使用utf-8存储
  2. 用字节流读取文件
    public static void main(String[] args) {
        InputStream inputStream=null;
        try {
            inputStream = new FileInputStream(new File("d:/test2.txt"));
            int result=0;
           while((result=inputStream.read())!=-1){
                System.out.println(result);
            }
        } catch (FileNotFoundException fileNotFoundException){
            fileNotFoundException.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }finally{
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ioException) {
                ioException.getMessage();
            }
        }
    }
  1. 调试查看result的值,发现他的值分别为 0xE6 , 0x88 和 0x91, utf-8编码一个汉字占3个字节没错,结果没什么问题,但是当时我就有了个疑惑,一个汉字3个字节,而字符流中一个char才2个字节,它怎么读的?
  2. 用字符流读取文件
public static void main(String[] args) {
            Reader reader=null;
            try {
                reader = new FileReader(new File("d:/test2.txt"));
                int result=0;
                while((result=reader.read())!=-1){
                    System.out.println(result);
                }
            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if (reader!= null) {
                        reader.close();
                    }
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
  1. 查看控制台输出了”我“,成功了,怎么回事,说好的1个char2个字节呢,这个汉字占了3个字节,怎么不按套路???
  2. 调试查看result的值,发现result的值为0x6211,而0x6211正好是汉字“我”的Unicode的值
  3. 因为字符流是使用字符数组读取的,而一个字符只能容纳2个字符,所以它存储的是字符的Unicode值,而不是字符在磁盘中存储的值。
  4. 也就是说如果一个文件使用非Unicode编码存储时,字符流读取会乱码,要想读取非Unicode文本文件,可以使用以下代码,其中gbk可以切换成你想要的编码方式
  5. reader = new InputStreamReader(new FileInputStream(new File("d:/test2.txt")),"gbk");
    本人Java小白,以上内容可能有错误,如发现希望指明,共同进步。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值