Read a file from the internet Tag(s): Networking IO


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

public class NetUtils {

  private NetUtils() {}

  public static String getTextContent(URL url) throws IOException {
      Scanner s = new Scanner(url.openStream()).useDelimiter("\\Z");;
      String content = s.next();
      return content;
  }

  public static void main(String[] args) throws IOException {
    URL url = new URL("http://www.rgagnon.com/varia/copyrightnotice.txt");
    System.out.println(NetUtils.getTextContent(url));
  }
}

blog comments powered by Disqus