1 Reply Latest reply: Jul 3, 2013 2:49 AM by 877802 RSS

    Chinese characters are printing in '?????'

    877802

      Hello All,

       

      We are trying to read a data in Chinese language from a text file. When trying to print the read data, we are seeing in ??????.

       

      Below is snippet code for that :

       

      {code}

      /*

        Read file using FileInputStream

        This example shows how to read a file using Java FileInputStream class.

        FileInputStream is used to read binary content of the file and

        return bytes of data.

      */

      import java.io.*;

       

      import java.nio.charset.Charset;

       

      import java.io.PrintStream;

      import java.io.UnsupportedEncodingException;

       

      public class ReadStringFromFile {

       

          public static void main(String[] args) {

       

              //create file object

              File file = new File("C:\\Users\\pneralla.ORADEV\\Desktop\\oneword_chinese.txt");

       

              int ch;

              StringBuffer strContent = new StringBuffer("");

              FileInputStream fin = null;

       

              try {

                  fin = new FileInputStream(file);

                  InputStreamReader isr = new InputStreamReader(fin, "UTF-8");

                  BufferedReader br = new BufferedReader(isr);

                  System.out.println("File contents ::");

                  String line = br.readLine();

                  System.out.println("Line :: " + line);

                 

                  /* We tried below scenarios and not able to see chinese actual font and seeing other font. */

                  PrintStream out = new PrintStream(System.out, true, "UTF-8");

                  System.setOut(out);

                  System.out.println("UTF-8 decoded: " + new String(line.getBytes("UTF-8"), "UTF-8"));           

              } catch (Exception e) {

                  e.printStackTrace();

              }

       

       

          }

       

       

      }

       

      {code}

       

      Output

      ----------

       

      File contents ::

      Line is :: ?????

      UTF-8 decoded: ��类型

       

       

      Thanks,

      Niranjan.