Reputation: 175
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();
}
}
}
}
Upvotes: 9
Views: 4607
Reputation: 101072
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.
Upvotes: 17