java - 为什么 FileReader 流从文本文件中读取 237、187、191?

标签 java inputstream

我有一个文本文件,里面只有一个字符“T”,我创建了一个读取流以将正在读取的内容输出到控制台,我得到了 239、187、191 和 84,我知道 84 代表'T',我知道 239、187、191 也代表其他字符,但我的文本文件中没有这些字符,这是怎么回事??

public class Test {

   public static void main(String args[]) throws IOException {  
      FileInputStream in = null;

      try {
         in = new FileInputStream("input.txt");         
         int c;
         while ((c = in.read()) != -1) {
             System.out.println(c);

         }
      }finally {
         if (in != null) {
            in.close();
         }
      }
   }
}

最佳答案

你确定不是239 187 191吗? (EF BB BF 十六进制)

您看到的是 byte order mark文件的:

The byte order mark (BOM) is a Unicode character, U+FEFF BYTE ORDER MARK (BOM), whose appearance as a magic number at the start of a text stream can signal several things to a program reading the text:

The byte order, or endianness, of the text stream;
The fact that the text stream's encoding is Unicode, to a high level of confidence;
Which Unicode encoding the text stream is encoded as.

关于java - 为什么 FileReader 流从文本文件中读取 237、187、191?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53303571/

相关文章:

java - 读取文件时遇到问题,程序看起来不错,但不明白为什么它输出 null

java - 创建 CSV 文件而不将其保存到文件系统中

java - 在 Java 8 中重用流

java - 参与者数量有限的 Firebase 房间

java - 在服务器运行时将表内容从 Oracle DB 加载到 Spring Bean

java - 使用 getBinaryStream 读取单元格

java - Cloud Stream中的@EnableBinding后面是做什么的?

java - 这段Java代码中如何处理堆内存不足的情况?

c++ - 使空间与 C++ 输入流完美配合

java - 如何在 Java 中向进程发送密码