Here you can find the source of readURLasString(URL url)
Parameter | Description |
---|---|
IOException | an exception |
null
if an error occured or the file doesn't exists.public static String readURLasString(URL url) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; public class Main { /**//w w w .j a v a 2 s. c o m * @return a {@link String} with the content of the {@link URL}. Do not use * this on long files! Returns <code>null</code> if an error occured * or the file doesn't exists.<br/> * A newline-character is added at every new line. * @throws IOException */ public static String readURLasString(URL url) throws IOException { InputStream openStream = url.openStream(); try { InputStreamReader inStream = new InputStreamReader(openStream); try { BufferedReader inReader = new BufferedReader(inStream); try { String oneLine = inReader.readLine(); if (oneLine == null) return ""; StringBuffer content = new StringBuffer(); while (oneLine != null) { content.append(oneLine); oneLine = inReader.readLine(); if (oneLine != null) content.append("\n"); } return content.toString(); } finally { inReader.close(); } } finally { inStream.close(); } } finally { openStream.close(); } } }