Java URL to copyUrlToFile(URL url, File file)

Here you can find the source of copyUrlToFile(URL url, File file)

Description

Copies the content of the given url to the specified file.

License

Apache License

Declaration

public static void copyUrlToFile(URL url, File file) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.Closeable;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;

import java.io.OutputStream;

import java.net.URL;

import java.util.zip.ZipFile;

public class Main {
    /**/*from   ww  w  . j  av a  2  s .  com*/
     * Copies the content of the given url to the specified file.
     */
    public static void copyUrlToFile(URL url, File file) {
        final InputStream inputStream;
        final FileOutputStream fileOutputStream;
        try {
            inputStream = url.openStream();
            fileOutputStream = new FileOutputStream(file);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        copy(inputStream, fileOutputStream);
        closeQuietly(inputStream);
        closeQuietly(fileOutputStream);
    }

    /**
     * Copies the content of the given input stream to a specified output
     * stream.
     */
    public static void copy(InputStream in, OutputStream out) {
        final byte[] buf = new byte[1024];
        int len;
        try {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Closes the specified closeable object, ignoring any exceptions.
     */
    public static void closeQuietly(Closeable... closeables) {
        for (final Closeable closeable : closeables) {
            try {
                closeable.close();
            } catch (final Exception e) {
                // Ignored
            }
        }
    }

    /**
     * Close the specified object input, ignoring any exceptions.
     */
    public static void closeQuietly(ObjectInput... objectInputs) {
        for (final ObjectInput objectInput : objectInputs) {
            try {
                objectInput.close();
            } catch (final Exception e) {
                // Ignored
            }
        }
    }

    /**
     * Same as {@link ZipFile#close()} but throwing only unchecked exceptions.
     */
    public static void closeQuietly(ZipFile... zipFiles) {
        for (final ZipFile zipFile : zipFiles) {
            try {
                zipFile.close();
            } catch (final IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * Equivalent to {@link InputStream#read()} but throwing only unchecked
     * exceptions.
     */
    public static int read(InputStream inputStream) {
        try {
            return inputStream.read();
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns the content of the given url as a string.
     */
    public static String read(URL url) {
        InputStream is;
        try {
            is = url.openStream();
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        try {
            return readAsString(is);
        } finally {
            closeQuietly(is);
        }
    }

    /**
     * Returns the content of the given input stream as a single string.
     */
    public static String readAsString(InputStream in) {
        final BufferedReader reader = new BufferedReader(
                new InputStreamReader(in));
        final StringBuilder out = new StringBuilder();
        final String newLine = System.getProperty("line.separator");
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                out.append(line);
                out.append(newLine);
            }
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        return out.toString();
    }
}

Related

  1. convertUrlToFilename(URL url)
  2. convertUrlToFilePath(URL url)
  3. convertUrlToHostNameAsNodeName(String url)
  4. convertUrlToMp3Cmd(final String url)
  5. copyUrlToFile(URL url, File file)