Java URL Load readURLText(URL url, StringBuffer errorText)

Here you can find the source of readURLText(URL url, StringBuffer errorText)

Description

Returns the raw text (i.e.

License

Open Source License

Parameter

Parameter Description
url the url of the web age to read as plain text.
errorText a custom message to return if something goes wrong.

Return

a StringBuffer containing the raw html text

Declaration


public static StringBuffer readURLText(URL url, StringBuffer errorText) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.URL;

public class Main {
    /**/*from  w w  w .j a v  a 2  s.c om*/
     * Returns the raw text (i.e. with tags as "\<...\>" strings) of a web page
     *
     * @param url the url of the web age to read as plain text.
     * @return a StringBuffer containing the raw html text
     */

    public static StringBuffer readURLText(URL url) {
        return readURLText(url, new StringBuffer("error: can't read URL "
                + url.toString()));
    }

    /**
     * Returns the raw text (i.e. with tags as "\<...\>" strings) of a web page
     *
     * @param url       the url of the web age to read as plain text.
     * @param errorText a custom message to return if something goes wrong.
     * @return a StringBuffer containing the raw html text
     */

    public static StringBuffer readURLText(URL url, StringBuffer errorText) {
        StringBuffer page = new StringBuffer("");
        String thisLine;
        try {
            BufferedReader source = new BufferedReader(
                    new InputStreamReader(url.openStream()));

            while ((thisLine = source.readLine()) != null) {
                page.append(thisLine + "\n");
            }
            return page;
        } catch (Exception e) {
            return errorText;
        }
    }
}

Related

  1. readUrlProperties(URL url)
  2. readUrlStream(String urlString)
  3. readUrlText(final URL url, final String encoding)
  4. readURLText(String urlPath, String encoding)
  5. readUrlText(String urlString)
  6. readURLThrowException(final String url)
  7. readURLToByteArray(URL url)
  8. readURLToString(URL u, String encoding)
  9. saveFile(final URL url, final File file)