Java URL Copy copyResource(URL resource, File destination)

Here you can find the source of copyResource(URL resource, File destination)

Description

copy Resource

License

Apache License

Declaration

public static void copyResource(URL resource, File destination) throws IOException 

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.net.URL;

public class Main {
    public static void copyResource(URL resource, File destination) throws IOException {
        if (resource == null) {
            throw new IllegalArgumentException("URL must not be null.");
        }//from www .  ja v a 2  s .  c om
        if (destination == null) {
            throw new IllegalArgumentException("URL must not be null.");
        }
        try (InputStream inStream = resource.openStream()) {
            File parent = destination.getParentFile();
            if ((parent != null) && (!parent.exists())) {
                if (!parent.mkdirs()) {
                    throw new IOException("Could not create target directory '" + parent + "'.");
                }
            }
            try (FileOutputStream outStream = new FileOutputStream(destination)) {
                byte[] buffer = new byte[1024];
                int amount;
                while ((amount = inStream.read(buffer)) >= 0) {
                    outStream.write(buffer, 0, amount);
                }
            }
        }
    }
}

Related

  1. copy_url_to_file(String url, String filename)
  2. copyFile(URL url, String destFilePath)
  3. copyFromURL(final String query, final File target)
  4. copyRemoteFile(URL url, File localFile)
  5. copyResource(URL from, File to)
  6. copyUrlBinaryFile(String fileName, String sourceUrlPath, String destPath)
  7. copyURLToFile(final URL source, final File destination)
  8. copyURLToFile(URL source, File destination)