utf 8 - Chinese Strings Handling in Java? -


this question has answer here:

in assigned project, original author has written function:

  public string asstring() throws dataexception   {     if (getdata() == null) return null;      charbuffer charbuf = null;      try     {         charbuf = s_charset.newdecoder().decode(bytebuffer.wrap(f_data));     }     catch (charactercodingexception e)     {         throw new dataexception("you can't have string parasolblob: " + this, e);     }      return charbuf.tostring()+"你好";  }    

please note constant s_charset defined as:

private static final charset s_charset = charset.forname("utf-8"); 

please note have hard-coded chinese string in return string.

now when program flow reaches method, throw following exception:

 java.nio.charset.unmappablecharacterexception: input length = 2 

and more interstingly, hard-coded chinese strings shown "??" @ console if system.out.println().

i think problem quite interesting in regard of localization. , i've tried changing charset.forname("gbk");

but seems not solution. also, have set coding of java class of "utf-8".

any experts have experience in regard? please share little? in advance!

and more interstingly, hard-coded chinese strings shown "??" @ console if system.out.println().

system.out performs transcoding operations utf-16 strings default jre character encoding. if not match encoding used device receiving character data corrupted. so, console should set use right character encoding(utf-8) render chinese chars properly.

if using eclipse can change console encoding going

run configuration-> common -> encoding(slect utf-8 dropdown)

enter image description here


Comments