import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.Arrays; 
 
public class Test1 { 
//用字节流读取中文 
        public static void main(String[] args) throws IOException { 
                //一个中文是2个字节,且每个字节都小于0 
                try(FileInputStream fis = new FileInputStream("file.txt")) { 
                        //定义一个int x 用来接收 
                        int x; 
                        while((x = fis.read()) != -1) { 
                                //判断是英文字符还是中文,英文直接输出 
                                byte b1 = (byte)x; 
                                if(b1 > 0) {                                                                     
                                        System.out.print((char)b1);//强转为char类型 
                                } else { 
                                        //如果 x < 0 则为中文就继续读取下一个字节, 将两个byte存入数组 
                                        byte b2 = (byte)fis.read(); 
                                        byte[] arr = {b1, b2}; 
                                        //将数组转化为字符串 
                                        String str = new String(arr); 
                                        System.out.print(str); 
                                } 
                        } 
                } 
 
        } 
 
} 
 
// 上面代码中的while循环体中的if语句判断条件, 如果我将 b1 > 0 改为 x > 0, 就不会输出中文,而是一堆问号, 但是我感觉b1 和 x没区别啊, 这是什么原因! 
 |