Java FileInputStream Copy copyFile(final File srcPath, final File dstPath)

Here you can find the source of copyFile(final File srcPath, final File dstPath)

Description

copy File

License

Apache License

Declaration

public static void copyFile(final File srcPath, final File dstPath) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w w  w  .  j  a va 2s.c  o  m*/
 * Copyright 2015 www.seleniumtests.com
 * 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.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    public static void copyFile(final File srcPath, final File dstPath) throws IOException {

        if (srcPath.isDirectory()) {
            if (!dstPath.exists()) {
                dstPath.mkdir();
            }

            String[] files = srcPath.list();
            for (String file : files) {
                copyFile(new File(srcPath, file), new File(dstPath, file));
            }
        } else {
            if (!srcPath.exists()) {
                throw new IOException("Directory Not Found ::: " + srcPath);
            } else {
                InputStream in = null;
                OutputStream out = null;
                try {
                    if (!dstPath.getParentFile().exists()) {
                        dstPath.getParentFile().mkdirs();
                    }

                    in = new FileInputStream(srcPath);
                    out = new FileOutputStream(dstPath);

                    // Transfer bytes
                    byte[] buf = new byte[1024];
                    int len;

                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    if (out != null) {
                        try {
                            out.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public static void copyFile(final String srcPath, final String dstPath) throws IOException {
        copyFile(new File(srcPath), new File(dstPath));
    }
}

Related

  1. copyFile(final File source, final File dest)
  2. copyFile(final File source, final File target)
  3. copyFile(final File src, final File dest)
  4. copyFile(final File src, final File dst)
  5. copyFile(final File srcFile, final File destFile)
  6. copyFile(final InputStream in, final File destFile)
  7. copyFile(final String from, final String to)
  8. copyFile(final String sourceFile, final String destinationFile)
  9. copyFile(final String sourceFilePath, final String destFilePath)