1

我试图让它在mac上工作,它在linux上工作得很好。

public class URLTest {

public static void main(String[] args) {
    try{
        String webpage="Insert random webpage here";
        InputStream in = new URL(webpage).openConnection().getInputStream();   
        InputStreamReader reader = new InputStreamReader(in);
        while(reader.ready()) 
            System.out.print((char)reader.read());

    }catch (IOException e){
        ;
    }
}

在 Mac 上,我只得到数字作为输出,而在 Windows 上,我什么也得不到。有什么想法可以在所有系统上运行吗?

干杯

4

1 回答 1

1

您应该定义一个字符集,如果不定义,它将以平台默认字符集加载,这对于不同的平台和语言是不同的。

试试这个,以 UTF-8 读取:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class URLTest {

    public static void main(String[] args) {
        try {
            String webpage = "Insert random webpage here";
            InputStream in = new URL(webpage).openConnection().getInputStream();
            InputStreamReader reader = new InputStreamReader(in, "UTF-8");
            while (reader.ready())
                System.out.print((char) reader.read());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2015-05-21T13:51:51.327 回答