Java URL Download downloadString(URL url)

Here you can find the source of downloadString(URL url)

Description

Downloads data from the given URL and returns it as a string.

License

Apache License

Parameter

Parameter Description
url the URL to fetch data from.

Exception

Parameter Description
IOException if an error occurs when reading from the stream.

Return

the data downloaded from the given URL as a string.

Declaration

public static String downloadString(URL url) throws IOException 

Method Source Code

//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() : "";
    }
}

Related

  1. downloadImg(String urlPath, File file)
  2. downloadMobiFromEpubUrl(String href)
  3. downloadPageSource(String stringURL)
  4. downloadSource(PrintStream out, PrintStream err, String srcURL, String dirStr, boolean extract)
  5. downloadString(String url)
  6. downloadString(URL url)
  7. downloadText(String url)
  8. downloadTextFromUrl(String url)
  9. downloadTo(String url, String filename)