Java InputStreamReader,mac 和 linux 上的输出不同

Java InputStreamReader, different output on mac and linux

我试图让它在 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 上,我什么也得不到。有什么想法可以让它在所有系统上运行吗?

干杯

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

试试这个,以 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();
        }
    }
}