Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Certain Chinese Characters not displayed properly.

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Certain Chinese Characters not displayed properly.

    We are retrieving Chinese data from a file and displaying it to our web page. However, for some reason, certain chinese characters are destroyed, thus resulting to 3 different characters instead of 1 character.

    I found out that:
    If we directly copy the bytes from the file into a new file, the chinese chars can be viewed properly in our web browser.
    If we save the bytes into a new String, the chinese is displayed properly but 1 or 2 characters are destroyed (displaying 3 different garbage characters each).

    Any idea on how to resolve this? Any help will be appreciated.

    Thanks and Regards,
    Kerwin


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Certain Chinese Characters not displayed properly.

    Hello,

    Make sure your file is stored using UTF-8 then open this file using the following snippet of code.

        public String readUTF8FileAsString(final String filePath) {
            try {
                final StringBuilder stringBuilder = new StringBuilder();
                final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
     
                String line;
     
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuilder.append(line);
                }
     
                bufferedReader.close();
     
                return stringBuilder.toString();
     
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            return null;
        }

    // Json