Java URL Load readPage(URL url)

Here you can find the source of readPage(URL url)

Description

Read from a page

License

Open Source License

Parameter

Parameter Description
url The URL to read from

Exception

Parameter Description
IOException If an error occurred

Return

The contents, with lines separated by \n

Declaration

public static String readPage(URL url) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w w w  .ja  v  a2s  . c  o m
 * RS2Lite, the open source Runescape Launcher
 * Copyright (C) 2012 Nikki <nikki@nikkii.us>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Read from a page
     * @param url
     *          The URL to read from
     * @return
     *          The contents, with lines separated by \n
     * @throws IOException
     *          If an error occurred
     */
    public static String readPage(URL url) throws IOException {
        return readFromInput(url.openStream());
    }

    /**
     * Read the contents of an input stream
     * @param input
     *          The input stream to read from
     * @return
     *          The contents, with lines separated by \n
     * @throws IOException
     *          If an error occurred while reading
     */
    public static String readFromInput(InputStream input) throws IOException {
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        try {
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }

                builder.append(line).append("\n");
            }
        } finally {
            reader.close();
        }
        return builder.toString();
    }
}

Related

  1. readListFile(URL listFile)
  2. readMappingFile(final URL filename)
  3. readModelFromUrl(URL file, Class clazz)
  4. readNetFile(String urlstr)
  5. readPage(final String urlStr)
  6. readProperties(final URL url)
  7. readProperties(final URL url)
  8. readProperties(URL resource)
  9. readPropertyFile(URL url)