Here you can find the source of readText(final URL url)
Parameter | Description |
---|---|
url | The URL to read |
Parameter | Description |
---|---|
IOException | When text could not be read. |
public static String readText(final URL url) throws IOException
//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(); } } }