Language support

T

tester

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();
}
}
 
N

Neal Gafter

tester said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top