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:


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

  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.
      // 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");
    catch(Exception e)
    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.

Share it!

There’s just one person behind this website; if this article was helpful (or interesting), I’d appreciate it if you’d share it. Thanks, Al.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.