Java URL to copyUrlToFile(URL url, File file)

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

Description

copy Url To 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.File;

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

import java.io.ObjectInput;

import java.io.OutputStream;

import java.io.Reader;
import java.io.Writer;
import java.net.URL;

public class Main {
    public static void copyUrlToFile(URL url, File file) {
        final InputStream inputStream;
        final FileOutputStream fileOutputStream;
        try {/* ww  w. ja  va2  s . c  o  m*/
            inputStream = url.openStream();
            fileOutputStream = new FileOutputStream(file);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        copy(inputStream, fileOutputStream);
        closeQuietly(inputStream);
        closeQuietly(fileOutputStream);
    }

    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);
        }
        closeQuietly(in);
        closeQuietly(out);
    }

    /**
     * Close the specified input stream, ignoring any exceptions.
     */
    public static void closeQuietly(InputStream inputStream) {
        try {
            inputStream.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

    /**
     * Close the specified writer, ignoring any exceptions.
     */
    public static void closeQuietly(Writer writer) {
        try {
            writer.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

    /**
     * Close the specified reader, ignoring any exceptions.
     */
    public static void closeQuietly(Reader reader) {
        try {
            reader.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

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

    /**
     * Close the specified output stream, ignoring any exceptions.
     */
    public static void closeQuietly(OutputStream outputStream) {
        try {
            outputStream.close();
        } catch (final Exception e) {
            // Ignored
        }
    }

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

Related

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