Here you can find the source of downloadString(URL url)
Parameter | Description |
---|---|
url | the URL to fetch data from. |
Parameter | Description |
---|---|
IOException | if an error occurs when reading from the stream. |
public static String downloadString(URL url) throws IOException
//package com.java2s; /*//from w ww. j a v a 2s.com * This file is part of FTB Launcher. * * Copyright ? 2012-2013, FTB Launcher Contributors <https://github.com/Slowpoke101/FTBLaunch/> * FTB Launcher is 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.IOException; import java.io.InputStream; import java.net.URL; import java.util.Scanner; public class Main { /** * Downloads data from the given URL and returns it as a string. * @param url the URL to fetch data from. * @return the data downloaded from the given URL as a string. * @throws IOException if an error occurs when reading from the stream. */ public static String downloadString(URL url) throws IOException { return readString(url.openStream()); } /** * Reads all of the data from the given stream and returns it as a string. * @param stream the stream to read from. * @return the data read from the given stream as a string. */ public static String readString(InputStream stream) { Scanner scanner = new Scanner(stream).useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : ""; } }