Java URL Copy copy(URL source, String destination)

Here you can find the source of copy(URL source, String destination)

Description

copy the file of a given URL to the given destination

License

Apache License

Parameter

Parameter Description
source URL: the URL of the source file
destination String: the filename of the destination

Declaration

public static void copy(URL source, String destination) 

Method Source Code


//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.
        }
    }
}

Related

  1. copy(URL fromUrl, File toFile)
  2. copy(URL in, File out)
  3. copy_url_to_file(String url, String filename)
  4. copyFile(URL url, String destFilePath)
  5. copyFromURL(final String query, final File target)
  6. copyRemoteFile(URL url, File localFile)