slides/URLReader.java
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 16 Nov 2016 18:48:00 +0000
changeset 52 7a4fe3f6b188
parent 32 45557ad18ea6
permissions -rw-r--r--
updated

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

public class URLReader {

    public static String readURL(String sUrl) {
        StringBuilder buf = new StringBuilder();
        Scanner in = null;

        try {
            URL url = new URL(sUrl);
            in = new Scanner(url.openStream());

            while (in.hasNextLine()) {
                buf.append(in.nextLine() + "\n");
            }
            return buf.toString();

        } catch (MalformedURLException e) {
            System.err.println(e);
        } catch (IOException e) {
            System.err.println(e);
        } finally {
            if (in != null) {
                in.close();
            }
        }       
        return null;
    }
}