Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the FileReader stream read 237, 187, 191 from a textfile?

I have a text file with a just a character 'T' inside, and I have created a read stream to output to the console what is being read and I got 239, 187, 191 and 84, I understand that 84 represents 'T', I know 239, 187, 191 represent other characters as well but I don't have those characters in my text file, what is going on??

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();
         }
      }
   }
}
like image 247
Mireodon Avatar asked Nov 14 '18 15:11

Mireodon


1 Answers

Are you sure it's not 239 187 191? (EF BB BF in hex)

You're seeing the byte order mark of the file:

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.

like image 134
sloth Avatar answered Nov 15 '22 01:11

sloth