Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-Detect Character Encoding in Java

Seems to be a fairly hit issue, but I've not yet been able to find a solution; perhaps because it comes in so many flavors. Here it is though. I'm trying to read some comma delimited files (occasionally the delimiters can be a little bit more unique than commas, but commas will suffice for now).

The files are supposed to be standardized across the industry, but lately we've seen many different types of character set files coming in. I'd like to be able to set up a BufferedReader to compensate for this.

What is a pretty standard way of doing this and detecting whether it was successful or not?

My first thoughts on this approach are to loop through character sets simple->complex until I can read the file without an exception. Not exactly ideal though...

Thanks for your attention.

like image 952
Kirk Avatar asked Feb 07 '12 18:02

Kirk


People also ask

Does Java use UTF-8 or UTF-16?

The native character encoding of the Java programming language is UTF-16.

What is charset defaultCharset ()?

defaultCharset. public static Charset defaultCharset() Returns the default charset of this Java virtual machine. The default charset is determined during virtual-machine startup and typically depends upon the locale and charset of the underlying operating system.


1 Answers

The Mozilla's universalchardet is supposed to be the efficient detector out there. juniversalchardet is the java port of it. There is one more port. Read this SO for more information Character Encoding Detection Algorithm

like image 99
Aravind Yarram Avatar answered Oct 10 '22 21:10

Aravind Yarram