Here you can find the source of readUrl(final URL url)
Parameter | Description |
---|---|
url | the url to be read |
Parameter | Description |
---|---|
IOException | thrown on problems while reading. |
public static String readUrl(final URL url) throws IOException
//package com.java2s; /**//from w ww. j av a2 s. co m * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; public class Main { /** * Reads an url to string; should only be used for non-blocking connections * (f.e. jar file contents and other things). * * @param url the url to be read * @return the results * @throws IOException thrown on problems while reading. */ public static String readUrl(final URL url) throws IOException { final InputStream stream = url.openStream(); final BufferedReader in = new BufferedReader(new InputStreamReader(stream)); String inputLine; final StringBuffer result = new StringBuffer(); while ((inputLine = in.readLine()) != null) { if (result.length() > 0) { result.append("\n"); } result.append(inputLine); } in.close(); return result.toString(); } }