How to find out the encoding of a text file
There is a program that processes a text file. You need to check the text encoding before opening and working with the file. Tell me how to implement it, I'm just starting to learn Java, I would appreciate a sample code.
3 answers
In general, this is not an easy task and I think it is not always possible to do it. Usually, the encoding is determined in advance. But really (as @metalurgus said) there is quite a lot of information on the net. Although, you need to understand that to solve such a problem, you will need to use some third-party library. I think this reasoning is suitable: encoding definition
Colleagues have already written that there is no single recipe, but I will still try to describe an approximate pattern for solving this problem:
1) Get the list of encodings supported by this platform
Charset.availableCharset()
2) Take the first one in the list charset
and read the line from the file:
BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(filePath), charset));
String line=br.readLine();
3) We take the Yandex Dictionary and make a JSon query lookup, remember the translation statistics
4) After running through all the available encodings, select the one that received the best statistics - this will be our desired encoding.
java.io.OutputStreamWriter.getEncoding()
the method returns the name of the character encoding used by the stream