You can't really, unless the file is kind enough to tell you somewhere inside it.
For example, HTML files are meant to contain a content-type meta tag near the top, so that your web browser knows what encoding is used.. eg
or
There are methods that try to guess by looking at the file and spotting byte sequences that suggest certain encodings, but these are really only guessing.