Here you can find the source of copy(URL source, String destination)
Parameter | Description |
---|---|
source | URL: the URL of the source file |
destination | String: the filename of the destination |
public static void copy(URL source, String destination)
//package com.java2s; //License from project: Apache License import java.io.*; import java.net.URL; public class Main { /**// w w w. j a va2 s . com * copy the file of a given URL to the given destination * * @param source URL: the URL of the source file * @param destination String: the filename of the destination */ public static void copy(URL source, String destination) { InputStream in = null; FileOutputStream out = null; try { in = source.openStream(); out = new FileOutputStream(destination); int c; byte[] buffer = new byte[2048]; int read = in.read(buffer); while (read != -1) { out.write(buffer, 0, read); read = in.read(buffer); } } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } if (out != null) try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * copy the file of a given URL to the given destination * * @param source String: the name of the source file * @param destination String: the filename of the destination */ public static void copy(String source, String destination) { FileInputStream in = null; FileOutputStream out = null; try { File inputFile = new File(source); File outputFile = new File(destination); in = new FileInputStream(inputFile); out = new FileOutputStream(outputFile); byte[] buffer = new byte[2048]; int read = in.read(buffer); while (read != -1) { out.write(buffer, 0, read); read = in.read(buffer); } } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException e) { e.printStackTrace(); } if (out != null) try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void close(Closeable fw) { try { fw.close(); } catch (Exception e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } }