Java URL Download downloadTextFromUrl(String url)

Here you can find the source of downloadTextFromUrl(String url)

Description

Downloads text from a given URL and returns it as a String

License

LGPL

Return

the text (or null if download failed)

Declaration

public static String downloadTextFromUrl(String url) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.IOException;
import java.io.InputStream;

import java.net.MalformedURLException;

import java.net.URL;
import java.util.Scanner;

public class Main {
    /**/*  w  w  w .  jav  a 2 s.c  o  m*/
     * Downloads text from a given URL and returns it as a String
     *
     * @return the text (or null if download failed)
     */
    public static String downloadTextFromUrl(String url) {
        InputStream in;
        try {
            in = new URL(url).openStream();
            Scanner scan = new Scanner(in);
            return scan.hasNext() ? scan.next() : null;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. downloadSource(PrintStream out, PrintStream err, String srcURL, String dirStr, boolean extract)
  2. downloadString(String url)
  3. downloadString(URL url)
  4. downloadString(URL url)
  5. downloadText(String url)
  6. downloadTo(String url, String filename)
  7. downloadToDirectory(URL url, String directoryName)
  8. downloadToFile(String url, File file)
  9. downloadToFile(URL source, File dest)