Java URL Open openReader(String aUrl)

Here you can find the source of openReader(String aUrl)

Description

convenience method for opening a buffered reader to an url, standard timeout is set to 500 milliseconds

License

Apache License

Parameter

Parameter Description
aUrl the url to open a reader to

Exception

Parameter Description
IOException if the url could not be read

Return

the buffered reader for reading the url

Declaration

public static BufferedReader openReader(String aUrl) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w  ww . java 2  s.c o m*/
 * Copyright 2014 Davy Maddelein.
 *
 * 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.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class Main {
    /**
     * convenience method for opening a buffered reader to an url, standard timeout is set to 500 milliseconds
     * @param aUrl the url to open a reader to
     * @return the buffered reader for reading the url
     * @throws IOException if the url could not be read
     */
    public static BufferedReader openReader(String aUrl) throws IOException {
        URL myURL = new URL(aUrl);
        HttpURLConnection c = (HttpURLConnection) myURL.openConnection();
        c.setConnectTimeout(500);
        return new BufferedReader(new InputStreamReader(new BufferedInputStream(c.getInputStream()), "UTF-8"));
    }
}

Related

  1. openStream(String aURL)
  2. openURL(URL source_url)
  3. openURLConnection(final URL couchdbURL)