slides/URLReader.java
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 28 Nov 2020 15:58:36 +0000
changeset 378 7a5ad01a85b5
parent 32 45557ad18ea6
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import java.io.IOException;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import java.net.MalformedURLException;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
import java.net.URL;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
import java.util.Scanner;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
public class URLReader {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
    public static String readURL(String sUrl) {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
        StringBuilder buf = new StringBuilder();
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
        Scanner in = null;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
        try {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
            URL url = new URL(sUrl);
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
            in = new Scanner(url.openStream());
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
            while (in.hasNextLine()) {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
                buf.append(in.nextLine() + "\n");
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
            }
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
            return buf.toString();
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
        } catch (MalformedURLException e) {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
            System.err.println(e);
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
        } catch (IOException e) {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
            System.err.println(e);
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
        } finally {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
            if (in != null) {
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
                in.close();
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
            }
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
        }       
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
        return null;
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
    }
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
}