Java Path Copy nio copyFiles(String urlDir, String outPath)

Here you can find the source of copyFiles(String urlDir, String outPath)

Description

Copy files in url directory to local path

License

LGPL

Parameter

Parameter Description
urlDir a parameter
outPath a parameter

Declaration

public static void copyFiles(String urlDir, String outPath) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    /**/*  ww w .  java 2  s.co  m*/
     * Copy files in url directory to local path
     *
     * @param urlDir
     * @param outPath
     */
    public static void copyFiles(String urlDir, String outPath) {
        copyFiles(urlDir, outPath, false);
    }

    public static void copyFiles(String urlDir, String outPath, boolean force) {
        getLinesLastElement(urlDir + "/").forEach(f -> {
            copyFile(urlDir + "/" + f, outPath + "/" + f, force);
        });
    }

    /**
     * Use this against a ftp directory to get listing of file names
     *
     * @param urlPath
     * @return
     */
    public static List<String> getLinesLastElement(String urlPath) {
        return getLines(urlPath).stream().map(s -> {
            String[] elms = s.split("\\s+");
            return elms[elms.length - 1];
        }).collect(Collectors.toList());
    }

    public static Boolean copyFile(String urlPath, String outFile) {
        return copyFile(urlPath, outFile, false);
    }

    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);
        }
    }

    /**
     * Get content of file
     *
     * @param urlPath
     * @return
     */
    public static List<String> getLines(String urlPath) {
        try {
            URL url = new URL(urlPath); // Directory listing
            URLConnection con = url.openConnection();
            try (BufferedReader buffer = new BufferedReader(
                    new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
                return buffer.lines().collect(Collectors.toList());
            }
        } catch (IOException ex) {
            // Unknown host or other network errors returns empty collection
            // Checking if online should be done in the application before
        }
        return new ArrayList<>();
    }

    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(String srcPath, String destPath)
  2. copyFile(String urlPath, String outFile)
  3. copyFileFromResourcesToServer(String resourceFile, Path targetDirectory, boolean override)
  4. copyFiles()
  5. copyFiles(String srcPath, String destPath)
  6. copyFilesAndApplyPermissions(Path sourceDir, Path targetDir, List filenames)
  7. copyFilesRecursively(final Path from, final Path to)
  8. copyFileToArchive(InputStream srcInputStream, Path destPath)
  9. copyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace)