1

Im trying to get this to work on a mac, it works just fine on 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){
        ;
    }
}

On mac I just get numbers as output and on windows I get nothing. Any idea to get this working on all systems?

Cheers

Thorstennn
  • 13
  • 3

1 Answers1

1

You should have a charset defined, if you don't it will be loaded in platform default charset, which is different for different platforms and languages.

Try this, to read in 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();
        }
    }
}
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32