Java FileInputStream Copy copyFile(String src, String dst)

Here you can find the source of copyFile(String src, String dst)

Description

copy File

License

Apache License

Declaration

public static synchronized File copyFile(String src, String dst)
            throws IOException 

Method Source Code

//package com.java2s;
/*/*  w w  w  .j a  v  a2  s . com*/
 * Copyright (c) Members of the EGEE Collaboration. 2004. 
 * See http://www.eu-egee.org/partners/ for details on the copyright
 * holders.  
 *
 * 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 synchronized File copyFile(String src, String dst)
            throws IOException {
        File srcFile = new File(src);
        File dstFile = new File(dst);

        if (dstFile.isDirectory()) {
            dstFile = new File(dst + "/" + srcFile.getName());
        }

        dstFile.createNewFile();

        InputStream in = new FileInputStream(srcFile);
        OutputStream out = new FileOutputStream(dstFile);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.flush();

        in.close();
        out.close();

        return dstFile;
    }
}

Related

  1. copyFile(String src, String dest)
  2. copyFile(String src, String dest)
  3. copyFile(String src, String dest)
  4. copyFile(String src, String dest)
  5. copyFile(String src, String dest)
  6. copyFile(String src, String dst)
  7. copyFile(String src, String tar)
  8. copyFile(String src, String to)
  9. copyFile(String srcDir, String destDir)