Java Path Copy nio copy(Path sourcePath, Path destinationPath)

Here you can find the source of copy(Path sourcePath, Path destinationPath)

Description

Copy files.

License

Open Source License

Parameter

Parameter Description
sourcePath Path for source
destinationPath Path for destination

Exception

Parameter Description
IOException an exception

Declaration

private static void copy(Path sourcePath, Path destinationPath)
        throws Exception 

Method Source Code

//package com.java2s;
/*/*from  w ww  .java 2  s.  com*/
 *  Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class Main {
    /**
     * Copy files.
     *
     * @param sourcePath      Path for source
     * @param destinationPath Path for destination
     * @throws IOException
     */
    private static void copy(Path sourcePath, Path destinationPath)
            throws Exception {
        String basedir = System.getProperty("basedir");
        if (basedir == null) {
            basedir = Paths.get(".").toString();
        }

        sourcePath = Paths.get(basedir).resolve(sourcePath);
        destinationPath = getCarbonHome().resolve(destinationPath);

        createOutputFolderStructure(destinationPath);
        try {
            Files.copy(sourcePath, destinationPath,
                    StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            throw new Exception("Error occurred while copying '"
                    + sourcePath + "' file to " + destinationPath + ".", e);
        }
    }

    /**
     * Return carbon home path
     *
     * @return Path for carbon home
     */
    private static Path getCarbonHome() {
        return Paths.get(System.getProperty("carbon.home"));
    }

    /**
     * Create the directory structure.
     *
     * @param destinationPath Path to file copying destination
     * @throws IOException
     */
    private static void createOutputFolderStructure(Path destinationPath)
            throws Exception {
        Path parentPath = destinationPath.getParent();
        try {
            Files.createDirectories(parentPath);
        } catch (IOException e) {
            throw new Exception(
                    "Error occurred while creating the directory '"
                            + parentPath + "'.", e);
        }
    }
}

Related

  1. copy(Path inPath, Path outPath)
  2. copy(Path source, Path destination)
  3. copy(Path source, Path destination)
  4. copy(Path source, Path target)
  5. copy(Path source, Path target, CopyOption... options)
  6. copy(Path sourcePath, Path targetPath)
  7. copy(Path src, Path dest, List globPattern)
  8. copy(String filePath, String fileName)
  9. copyAlways(Path source, Path target)