Consider this: characters are really just numbers. You can open the text file and say, "I think this is ANSI." And then when you come across a number that you know is not in ANSI, the only thing you can say for sure is "normal, it's not ANSI."
You cannot automatically detect the encoding, but you may find that the encodings are probably incorrect. This way you can hypothesize coding, but you will never be completely sure.
source share