Java URL Download nio downloadFirstLineFromInternetQuietly(URL url)

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

Description

download First Line From Internet Quietly

License

Open Source License

Declaration

public static String downloadFirstLineFromInternetQuietly(URL url) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.Closeable;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.URL;

import java.nio.charset.Charset;

public class Main {
    public static String downloadFirstLineFromInternetQuietly(URL url) {
        BufferedReader reader = null;
        try {/*  ww w.j  av a2s  . c  o  m*/
            reader = new BufferedReader(new InputStreamReader(url.openStream(), Charset.forName("UTF-8")));
            return reader.readLine();
        } catch (IOException ioe) {
            return "";
        } finally {
            closeQuietly(reader);
        }
    }

    /**
     * Close a stream quietly because we honestly don't care if a stream.close()
     * throws IOException
     */
    public static void closeQuietly(Closeable cl) {
        if (cl == null) {
            return;
        }
        try {
            cl.close();
        } catch (IOException ioe) {
            // do nothing
        }
    }
}

Related

  1. downloadFile(URL downloadUrl, File destination)
  2. downloadFile(URL url, File output)
  3. downloadFile(URL url, File targetFile)
  4. downloadFileNIO(FileChannel fileChannel, SocketChannel socketChannel)
  5. downloadFileToDirectory(String url, File destination)
  6. downloadFromHttpUrl(String destPkgUrl, FileOutputStream outputStream)
  7. downloadFromInternet(URL url, File downloadTo)
  8. downloadImage(String src, Path saveFolder)
  9. downloadToFile(String filename, String urlString)