Here you can find the source of readURLToString(URL u, String encoding)
Parameter | Description |
---|---|
u | the URL |
encoding | the character encoding |
Parameter | Description |
---|---|
IOException | if the URL contents could not be read |
public static String readURLToString(URL u, String encoding) throws IOException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.io.IOException; import java.io.InputStream; import java.net.URL; public class Main { /**/*from ww w.j av a 2 s . c o m*/ * Reads a string from a URL * @param u the URL * @param encoding the character encoding * @return the string * @throws IOException if the URL contents could not be read */ public static String readURLToString(URL u, String encoding) throws IOException { return readStreamToString(u.openStream(), encoding); } /** * Reads a string from a stream. Closes the stream after reading. * @param is the stream * @param encoding the character encoding * @return the string * @throws IOException if the stream contents could not be read */ public static String readStreamToString(InputStream is, String encoding) throws IOException { try { StringBuilder sb = new StringBuilder(); byte[] buf = new byte[1024 * 10]; int read; while ((read = is.read(buf)) >= 0) { sb.append(new String(buf, 0, read, encoding)); } return sb.toString(); } finally { is.close(); } } }