Here you can find the source of downloadFileIfAvailable(URL url, File destination)
public static File downloadFileIfAvailable(URL url, File destination) throws IOException
//package com.java2s; /**// www. j av a2s . c o m * BioJava development code * * This code may be freely distributed and modified under the * terms of the GNU Lesser General Public Licence. This should * be distributed with the code. If you do not have a copy, * see: * * http://www.gnu.org/copyleft/lesser.html * * Copyright for this code is held jointly by the individual * authors. These should be listed in @author doc comments. * * For more information on the BioJava project and its aims, * or to join the biojava-l mailing list, visit the home page * at: * * http://www.biojava.org/ * * Created on Feb 23, 2012 * Created by Andreas Prlic * * @since 3.0.2 */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.URL; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class Main { public static File downloadFileIfAvailable(URL url, File destination) throws IOException { InputStream uStream = null; InputStream conn = null; try { uStream = url.openStream(); conn = new GZIPInputStream(uStream); } catch (IOException e1) { System.err.println("Problem while downloading file " + url); //e1.printStackTrace(); try { if (uStream != null) { uStream.close(); } if (conn != null) { conn.close(); } } catch (IOException e) { e.printStackTrace(); } return null; } FileOutputStream outPut = null; GZIPOutputStream gzOutPut = null; File tempFile = File.createTempFile(getFilePrefix(destination), "." + getFileExtension(destination)); try { outPut = new FileOutputStream(tempFile); gzOutPut = new GZIPOutputStream(outPut); PrintWriter pw = new PrintWriter(gzOutPut); BufferedReader fileBuffer = new BufferedReader( new InputStreamReader(conn)); String line; while ((line = fileBuffer.readLine()) != null) { pw.println(line); } pw.flush(); pw.close(); outPut.flush(); outPut.close(); conn.close(); uStream.close(); } catch (Exception e) { System.err.println("Problem while downloading " + url); e.printStackTrace(); return null; } finally { if (conn != null) { try { conn.close(); } catch (IOException e) { e.printStackTrace(); } } if (uStream != null) { try { uStream.close(); } catch (IOException e) { e.printStackTrace(); } } try { if (outPut != null) { outPut.close(); } if (gzOutPut != null) { gzOutPut.close(); } } catch (IOException e) { } } System.out.println("Writing to " + destination); copy(tempFile, destination); return destination; } public static String getFilePrefix(File f) { String fileName = f.getName(); String fname = ""; int mid = fileName.indexOf("."); fname = fileName.substring(0, mid); return fname; } public static String getFileExtension(File f) { String fileName = f.getName(); //String fname=""; String ext = ""; int mid = fileName.lastIndexOf("."); //fname=fileName.substring(0,mid); ext = fileName.substring(mid + 1, fileName.length()); //System.out.println("File name ="+fname); //System.out.println("Extension ="+ext); return ext; } /** Copy the content of file A to B * * @param src * @param dst * @throws IOException */ public static void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } }