Here you can find the source of saveFileFromURL(URL url, File destinationFile)
Parameter | Description |
---|---|
url | The url to get |
destinationFile | The file to write to |
Parameter | Description |
---|---|
IOException | if something goes wrong |
public static void saveFileFromURL(URL url, File destinationFile) throws IOException
//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(); } }