Java URL Load saveFileFromURL(URL url, File destinationFile)

Here you can find the source of saveFileFromURL(URL url, File destinationFile)

Description

Writes the contents of the entry at the given url to the given file.

License

Open Source License

Parameter

Parameter Description
url The url to get
destinationFile The file to write to

Exception

Parameter Description
IOException if something goes wrong

Declaration

public static void saveFileFromURL(URL url, File destinationFile) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
 * |                                                                         |
 *    faint - The Face Annotation Interface
 * |  Copyright (C) 2007  Malte Mathiszig                                    |
 * /*from   w  w w. j a  va  2  s  .  c om*/
 * |  This program is free software: you can redistribute it and/or modify   |
 *    it under the terms of the GNU General Public License as published by
 * |  the Free Software Foundation, either version 3 of the License, or      |
 *    (at your option) any later version.                                     
 * |                                                                         |
 *    This program is distributed in the hope that it will be useful,
 * |  but WITHOUT ANY WARRANTY; without even the implied warranty of         |
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * |  GNU General Public License for more details.                           |
 * 
 * |  You should have received a copy of the GNU General Public License      |
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * |                                                                         |
 * + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
 *******************************************************************************/

import java.io.File;
import java.io.FileOutputStream;

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

import java.net.URL;

public class Main {
    /**
     * Writes the contents of the entry at the given url to the given file.
     *
     * @param url The url to get
     * @param destinationFile The file to write to
     * @throws IOException if something goes wrong
     */
    public static void saveFileFromURL(URL url, File destinationFile) throws IOException {
        FileOutputStream fo = new FileOutputStream(destinationFile);
        InputStream is = url.openStream();

        byte[] data = new byte[1024];
        int bytecount = 0;
        do {
            fo.write(data, 0, bytecount);
            bytecount = is.read(data);
        } while (bytecount > 0);

        fo.flush();
        fo.close();
    }
}

Related

  1. readURLText(URL url, StringBuffer errorText)
  2. readURLThrowException(final String url)
  3. readURLToByteArray(URL url)
  4. readURLToString(URL u, String encoding)
  5. saveFile(final URL url, final File file)
  6. saveImage(String imageUrl, String destinationFile)
  7. saveImageAsFile(String imageUrl, String destinationFile, boolean overwrite)
  8. saveUrl(String filename, String urlString)
  9. saveUrl(String filename, URL url)