Java BufferedInputStream Copy copyFile(File origem, File destino)

Here you can find the source of copyFile(File origem, File destino)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File origem, File destino) throws IOException 

Method Source Code


//package com.java2s;
/*/*w  ww  .j a v  a 2 s .com*/
 * Copyright 2014, Luis Filipe Nassif
 * 
 * This file is part of MultiContentViewer.
 *
 * MultiContentViewer 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 3 of the License, or
 * (at your option) any later version.
 *
 * MultiContentViewer 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 MultiContentViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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 origem, File destino) throws IOException {
        copyFile(origem, destino, false);
    }

    public static void copyFile(File origem, File destino, boolean append) throws IOException {
        InputStream in = new BufferedInputStream(new FileInputStream(origem));
        OutputStream out = new BufferedOutputStream(new FileOutputStream(destino, append));
        if (append) {
            out.write(0x0A);
        }
        byte[] buf = new byte[1000000];
        int len;
        while ((len = in.read(buf)) >= 0 && !Thread.currentThread().isInterrupted()) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
        if (len != -1) {
            if (!destino.delete()) {
                throw new IOException("Unable to delete " + destino.getPath());
            }
        }
    }
}

Related

  1. copyFile(File in, File out)
  2. copyFile(File in, File out)
  3. copyFile(File inFile, File outFile)
  4. copyFile(File inFile, File outFile)
  5. copyFile(File inputFile, File outputFile)
  6. copyFile(File source, File dest)
  7. copyFile(File source, File dest, boolean deleteIfExists)
  8. copyFile(File source, File dest, boolean deleteIfExists)
  9. copyFile(File source, File destination)