Java URL Load readUrl(final URL url)

Here you can find the source of readUrl(final URL url)

Description

Reads an url to string; should only be used for non-blocking connections (f.e.

License

Apache License

Parameter

Parameter Description
url the url to be read

Exception

Parameter Description
IOException thrown on problems while reading.

Return

the results

Declaration

public static String readUrl(final URL url) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w  ww.  j  av  a2  s.  co m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.URL;

public class Main {
    /**
     * Reads an url to string; should only be used for non-blocking connections
     * (f.e. jar file contents and other things).
     * 
     * @param url the url to be read
     * @return the results
     * @throws IOException thrown on problems while reading.
     */
    public static String readUrl(final URL url) throws IOException {
        final InputStream stream = url.openStream();
        final BufferedReader in = new BufferedReader(new InputStreamReader(stream));
        String inputLine;
        final StringBuffer result = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            if (result.length() > 0) {
                result.append("\n");
            }
            result.append(inputLine);
        }
        in.close();
        return result.toString();
    }
}

Related

  1. readTextStream(URL url, String encoding)
  2. readTextURL(URL url)
  3. readURL(final URL fileURL)
  4. readURL(final URL url)
  5. readUrl(final URL url)
  6. readURL(String URL)
  7. readURL(String url)
  8. readURL(String url)
  9. readURL(String url, String charsetName)