Language support

Discussion in 'Java' started by tester, Aug 29, 2003.

  1. tester

    tester Guest

    Hi ladies and gentlemen. I have a question that I hope you would do me a
    favour. Thanks!

    I wrote a Java program which will get the content (or source code) of
    HTML pages. The content often contains Chinese. When I use Java 1.2.2 to
    compile and run the program, the Chinese can be displayed correctly. But
    If I compile and run by Java SDK 1.4.1, the Chinese will become question
    marks. Do you have any solution or advice to this problem? The source
    code of the program is shown below for reference:
    -------------------------------------

    import java.io.*;
    import java.net.*;

    public class TEST
    {
    public static void main (String args[]) throws Exception
    {

    URL url = new URL("http://www.google.com.hk/index.html");
    HttpURLConnection conn = (HttpURLConnection)
    url.openConnection();
    conn.setDoOutput(true);
    InputStream in = conn.getInputStream();

    String s;
    while (in.available() > 0)
    {
    byte[] b = new byte[1000];
    in.read(b);
    s = new String(b);
    System.out.println(s);
    }
    in.close();
    }
    }
    tester, Aug 29, 2003
    #1
    1. Advertising

  2. tester

    Neal Gafter Guest

    tester wrote:
    > I wrote a Java program which will get the content (or source code) of
    > HTML pages. The content often contains Chinese. When I use Java 1.2.2 to
    > compile and run the program, the Chinese can be displayed correctly. But
    > If I compile and run by Java SDK 1.4.1, the Chinese will become question
    > marks. Do you have any solution or advice to this problem? The source
    > code of the program is shown below for reference:


    ....

    > byte[] b = new byte[1000];
    > in.read(b);
    > s = new String(b);


    This is your problem. The constructor String(byte[]) is specified to use your
    platform's default encoding to translate the sequence of bytes into a sequence
    of characters. Look for another String constructor that uses an encoding that
    supports the charset appearing on input.
    Neal Gafter, Aug 29, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page