slides/URLReader.java
changeset 32 45557ad18ea6
equal deleted inserted replaced
31:d0caa12ab8d8 32:45557ad18ea6
       
     1 import java.io.IOException;
       
     2 import java.net.MalformedURLException;
       
     3 import java.net.URL;
       
     4 import java.util.Scanner;
       
     5 
       
     6 public class URLReader {
       
     7 
       
     8     public static String readURL(String sUrl) {
       
     9         StringBuilder buf = new StringBuilder();
       
    10         Scanner in = null;
       
    11 
       
    12         try {
       
    13             URL url = new URL(sUrl);
       
    14             in = new Scanner(url.openStream());
       
    15 
       
    16             while (in.hasNextLine()) {
       
    17                 buf.append(in.nextLine() + "\n");
       
    18             }
       
    19             return buf.toString();
       
    20 
       
    21         } catch (MalformedURLException e) {
       
    22             System.err.println(e);
       
    23         } catch (IOException e) {
       
    24             System.err.println(e);
       
    25         } finally {
       
    26             if (in != null) {
       
    27                 in.close();
       
    28             }
       
    29         }       
       
    30         return null;
       
    31     }
       
    32 }