Java BufferedInputStream Copy copyFile(String dest_path, String src_path)

Here you can find the source of copyFile(String dest_path, String src_path)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(String dest_path, String src_path) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w w w. ja v  a2  s.  co  m*/
 * Copyright (C) 2005 Jeff Tassin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void copyFile(String dest_path, String src_path) throws IOException {
        if (dest_path == null)
            return;

        try {
            File f1 = new File(dest_path);
            File f2 = new File(src_path);

            if (f1.getCanonicalPath().equals(f2.getCanonicalPath())) {
                System.err.println("FormsDesignerUtils.copyFile  dest and src are same.");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        FileInputStream fis = new FileInputStream(src_path);
        FileOutputStream fos = new FileOutputStream(dest_path);

        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] buff = new byte[1024];
        int numread = bis.read(buff);
        while (numread > 0) {
            bos.write(buff, 0, numread);
            numread = bis.read(buff);
        }

        bos.flush();
        bos.close();
        bis.close();
    }
}

Related

  1. copyFile(final File src, final File dest)
  2. copyFile(final File to, final File from)
  3. copyFile(InputStream is, File newFile)
  4. copyFile(InputStream src, File dest)
  5. copyFile(InputStream src, File dst)
  6. copyFile(String fileName, File sourceRoot, File targetRoot, Set copied)
  7. copyFile(String from, String to)
  8. copyFile(String fromFile, String toFile)
  9. copyFile(String source, String dest)