Java URL Load readTextStream(URL url, String encoding)

Here you can find the source of readTextStream(URL url, String encoding)

Description

read a text from a URL

License

Open Source License

Declaration

public static String readTextStream(URL url, String encoding) throws IOException 

Method Source Code


//package com.java2s;
/*/* w  ww  .java 2s . co m*/
 * This file is part of the Jose Project
 * see http://jose-chess.sourceforge.net/
 * (c) 2002-2006 Peter Sch?fer
 *
 * 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 2 of the License, or
 * (at your option) any later version.
 *
 */

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

public class Main {
    /**
    * read a text from a URL
    */
    public static String readTextStream(URL url, String encoding) throws IOException {
        InputStream in = url.openStream();
        return readTextStream(in, encoding);
    }

    /**
    * read a text input stream
    */
    public static String readTextStream(InputStream input, String encoding) throws IOException {
        InputStreamReader in = new InputStreamReader(input, encoding);
        StringWriter out = new StringWriter();
        copyReader(in, out);
        in.close();
        out.close();

        return out.toString();
    }

    /**
     * copies the contens of a stream
     */
    public static void copyReader(Reader in, Writer out) throws IOException {
        char[] buffer = new char[4096];
        for (;;) {
            int count = in.read(buffer, 0, 4096);
            if (count < 0)
                break;
            out.write(buffer, 0, count);
        }
    }
}

Related

  1. readText(final URL url)
  2. readText(final URL url)
  3. readTextFile(final URL resource)
  4. readTextFileAtUrl(final URL url)
  5. readTextFromUrl(URL url)
  6. readTextURL(URL url)
  7. readURL(final URL fileURL)
  8. readUrl(final URL url)
  9. readUrl(final URL url)