Java FileInputStream Copy copyFile(File from, File to)

Here you can find the source of copyFile(File from, File to)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File from, File to) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  ww w  . j  a  va  2 s  .  c om*/
 * IndiPlexManager
 * Copyright (C) 2012 IndiPlex
 * 
 * IndiPlexManager 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

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(File from, File to) throws IOException {
        if (!from.exists() || !to.exists()) {
            return;
        }
        copyAndCloseStrams(new FileInputStream(from), new FileOutputStream(to));
    }

    public static void copyAndCloseStrams(InputStream in, OutputStream out) throws IOException {
        copy(in, out);
        in.close();
        out.close();
    }

    public static void copy(InputStream in, OutputStream out) throws IOException {
        int r = in.read();
        while (r != -1) {
            out.write(r);
            r = in.read();
        }
    }
}

Related

  1. copyFile(File fileToCopy, File targetDir)
  2. copyFile(File from, File to)
  3. copyFile(File from, File to)
  4. copyFile(File from, File to)
  5. copyFile(File from, File to)
  6. copyFile(File from, File to)
  7. copyFile(File from, File to)
  8. copyFile(File from, File to)
  9. copyFile(File from, File to)