Tuesday, January 13, 2009

Correct encoding of String in Java

Found many examples of correct encoding/decoding of string in Java. Tried many ways, but helped only one. Here is an example of one program. The InputStreamReader reads stream that is encoded in the Cp1251. Program correctly reads and transforms to correct encoding that Java can represent it.

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "Cp1251"));

rd.readLine(); // Here is the magic :))

If you know Russian, there is a good article about encoding in Java: http://www.jug.ru/servlets/index?page=rus

Post a Comment