Java URL Load readText(final URL url)

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

Description

Reads text from the specified URL.

License

Open Source License

Parameter

Parameter Description
url The URL to read

Exception

Parameter Description
IOException When text could not be read.

Return

The read text as string

Declaration

public static String readText(final URL url) throws IOException 

Method Source Code


//package com.java2s;
/*//  w  w  w . j  ava2s.  c om
 * Copyright (C) 2010 Klaus Reimer <k@ailis.de>
 * See LICENSE.md for licensing information.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Main {
    /**
     * Reads text from the specified URL.
     *
     * @param url
     *            The URL to read
     * @return The read text as string
     * @throws IOException
     *             When text could not be read.
     */
    public static String readText(final URL url) throws IOException {
        if (url == null)
            throw new IOException("File not found");
        final InputStream stream = url.openStream();
        try {
            final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            final byte[] buffer = new byte[8192];
            int read;
            while ((read = stream.read(buffer)) > 0) {
                bytes.write(buffer, 0, read);
            }
            return bytes.toString("UTF-8");
        } finally {
            stream.close();
        }
    }
}

Related

  1. readSqlStatements(URL url)
  2. readStopwordsURL(URL url, boolean lowercase)
  3. readStringFromUrl(URL url)
  4. readStringFromURL(URL url)
  5. readText(final URL url)
  6. readTextFile(final URL resource)
  7. readTextFileAtUrl(final URL url)
  8. readTextFromUrl(URL url)
  9. readTextStream(URL url, String encoding)