Java FileInputStream Copy copyFile(File src, File dst)

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

Description

Copies a file

License

Open Source License

Parameter

Parameter Description
src Source file
dst Destination file

Declaration

public static void copyFile(File src, File dst) throws IOException 

Method Source Code


//package com.java2s;
/*/*from www  .j a v  a 2  s.  c om*/
  Copyright (c) 2006 Harri Kaimio
      
  This file is part of Photovault.
    
  Photovault is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
    
  Photovault 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
  General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with Photovault; if not, write to the Free Software Foundation,
  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

import java.io.*;

public class Main {
    /**
       Copies a file
       @param src Source file
       @param dst Destination file
    */
    public static void copyFile(File src, File dst) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
            byte buf[] = new byte[1024];
            int nRead = 0;
            int offset = 0;
            while ((nRead = in.read(buf)) > 0) {
                out.write(buf, 0, nRead);
                offset += nRead;
            }
        } catch (IOException e) {
            throw e;
        } finally {
            IOException outEx = null;
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ex) {
                    outEx = ex;
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
            if (outEx != null) {
                throw outEx;
            }
        }
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File dst)
  7. copyFile(File src, File dst, boolean copyLMD)
  8. copyFile(File src, File target)
  9. copyFile(File src, File toDir)