Java Path Copy nio copyFile(String urlPath, String outFile)

Here you can find the source of copyFile(String urlPath, String outFile)

Description

copy File

License

LGPL

Declaration

public static Boolean copyFile(String urlPath, String outFile) 

Method Source Code

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

import java.io.File;
import java.io.IOException;

import java.io.UncheckedIOException;
import java.net.URL;
import java.net.URLConnection;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class Main {
    public static Boolean copyFile(String urlPath, String outFile) {
        return copyFile(urlPath, outFile, false);
    }//from ww  w.  j  av  a 2 s. c om

    public static Boolean copyFile(String urlPath, String outFile, boolean force) {
        if (!force && new File(outFile).exists()) {
            return false;
        }
        try {
            URL url = new URL(urlPath);
            URLConnection con = url.openConnection();
            Files.copy(con.getInputStream(), Paths.get(outFile), StandardCopyOption.REPLACE_EXISTING);
            return true;
        } catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
    }

    public static URLConnection openConnection(String urlPath) {
        try {
            URL url = new URL(urlPath);
            return url.openConnection();
        } catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
    }
}

Related

  1. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  2. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  3. copyFile(Path source, Path target, boolean prompt, boolean preserve)
  4. copyFile(Path srcFile, Path srcDir, Path dstDir)
  5. copyFile(String srcPath, String destPath)
  6. copyFileFromResourcesToServer(String resourceFile, Path targetDirectory, boolean override)
  7. copyFiles()
  8. copyFiles(String srcPath, String destPath)
  9. copyFiles(String urlDir, String outPath)