Java URL and URLConnection example - how to read content from a URL

Question: Using Java, how can I open a URL from my program, and then read the content of that URL?

Answer: One way to open a URL in Java is to use the URL and URLConnection classes. You can then read the data/content from the URL using a combination of an InputStreamReader and BufferedReader Here's a sample Java class that demonstrates the basic techniques:

import java.net.*;
import java.io.*;

/**
 * A complete Java class that demonstrates how to read content (text) from a URL
 * using the Java URL and URLConnection classes.
 * @author alvin alexander, alvinalexander.com
 */
public class JavaUrlConnectionReader
{
  public static void main(String[] args)
  {
    String output  = getUrlContents("http://localhost:8080/");
    System.out.println(output);
  }

  private static String getUrlContents(String theUrl)
  {
    StringBuilder content = new StringBuilder();

    // many of these calls can throw exceptions, so i've just
    // wrapped them all in one try/catch statement.
    try
    {
      // create a url object
      URL url = new URL(theUrl);

      // create a urlconnection object
      URLConnection urlConnection = url.openConnection();

      // wrap the urlconnection in a bufferedreader
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

      String line;

      // read from the urlconnection via the bufferedreader
      while ((line = bufferedReader.readLine()) != null)
      {
        content.append(line + "\n");
      }
      bufferedReader.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return content.toString();
  }
}

As you can see from that code the process of reading data from a URL with Java is pretty simple:

  1. Create a new Java URL object, passing in the desired URL you want to access.
  2. Use the URL object to create a Java URLConnection object.
  3. Read from the URLConnection using the InputStreamReader and BufferedReader.
  4. The BufferedReader readLine method returns a String that you can acccess. If this String is null, you've reached the end of the document.
  5. Append the series of strings that you receive as output from the URL to your StringBuilder object.

As you can also see, this getUrlContents method can easily be moved into a web utilities class, where you can re-use it as desired.