Java URL Load readURL(String url)

Here you can find the source of readURL(String url)

Description

Read data from a properly formatted URL and return it as a string.

License

Open Source License

Declaration

public static String readURL(String url) throws MalformedURLException, IOException 

Method Source Code


//package com.java2s;
// for Our Notice and the LICENSE file for the GNU Lesser General Public

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    private final static String EOL = "\n";

    /**/*from w  w  w.  jav  a  2 s . c o m*/
     * Read data from a properly formatted URL and return it as a string.
     * This method may throw a <code>MalformedURLException</code> if the
     * URL is improperly formatted or an <code>IOException</code> if there
     * is a problem reading the URL data.
     */
    public static String readURL(String url) throws MalformedURLException, IOException {
        InputStream is = new URL(url).openStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
            buffer.append(EOL);
        }
        is.close();

        return buffer.toString();
    }
}

Related

  1. readURL(final URL fileURL)
  2. readURL(final URL url)
  3. readUrl(final URL url)
  4. readUrl(final URL url)
  5. readURL(String URL)
  6. readURL(String url)
  7. readURL(String url, String charsetName)
  8. ReadURL(String URLAddress)
  9. readUrl(String urlString)