[Java] – How to read a file with correctly encoding charset ?>

[Java] – How to read a file with correctly encoding charset

Problem:

I try to read a .srt file and get some weird characters from it when print out to console.

Situation:

If a file was create by BOM included => you cannot read that file like normal.

Idea:

Detect which encoding charset of that file then try to read it by correctly charset.

Solution:

Google Data API has a class which support to get exactly encoding charset of a file, you can check it at here

From that class, I’ve created my own method to return the encoding charset like this:

And use it like this:

Have fun!

References:

http://stackoverflow.com/questions/34003360/weird-character-when-read-srt-file-from-text

http://stackoverflow.com/questions/1835430/byte-order-mark-screws-up-file-reading-in-java/1835577#1835577

Leave a Reply

Your email address will not be published. Required fields are marked *