Skip to content Skip to sidebar Skip to footer

Convert Shift_jis Format To Utf-8 Format

I am trying to convert a Shift_JIS formatted file into UTF-8 format. For this, below is my approach: Read Shift_JIS file getBytes of each line and convert it to UTF-8 Create new f

Solution 1:

The answer @VicJordan posted is not correct. When you call getBytes(), you are getting the raw bytes of the string encoded under your system's native character encoding (which may or may not be UTF-8). Then, you are treating those bytes as if they were encoded in UTF-8, which they might not be.

A more reliable approach would be to read the Shift_JIS file into a Java String. Then, write out the Java String using UTF-8 encoding.

InputStreamin= ...
Readerreader=newInputStreamReader(in, "Shift_JIS");
StringBuildersb=newStringBuilder();
int read;
while ((read = reader.read()) != -1){
  sb.append((char)read);
}
reader.close();

Stringstring= sb.toString();

OutputStreamout= ...
Writerwriter=newOutputStreamWriter(out, "UTF-8");
writer.write(string);
writer.close();

Solution 2:

Finally i found the solution. Was doing some very basic mistake. Below code is working perfectly fine:

InputStreaminputStream= getContentResolver().openInputStream(uri);
BufferedReaderreader=newBufferedReader(newInputStreamReader(inputStream, "Shift_JIS"));
byte[] b = line.getBytes();
Stringvalue=newString(b, "UTF-8");

Post a Comment for "Convert Shift_jis Format To Utf-8 Format"