Java字节流和字符流

file.txt文本中存储的内容:

好abc

1.字符流处理:

package com.wjy.java;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

 class Test {
    public static void main(String[] args){
        int byteEx;
try{
        FileReader inputStream=new FileReader("./file/file.txt");
        while((byteEx=inputStream.read())!=-1){
            System.out.println(byteEx);
        }
        
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

结果:

22909                      //汉字"好"的结果
97
98
99

2.字节流处理:

package com.wjy.java;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

 class Test {
    public static void main(String[] args){
        int byteEx;
        try{
        FileInputStream inputStream=new FileInputStream("./file/file.txt");
        while((byteEx=inputStream.read())!=-1){
            System.out.println(byteEx);
        }
        
        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

结果:

186                                  //汉字"好"的第一部分
195                                  //汉字”好“的第二部分
97
98
99
原文地址:https://www.cnblogs.com/wangjiyuan/p/javazijiezifu.html