Java BufferedInputStream Copy copyFile(File src, File dest)

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

Description

copy File

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*//from w  ww .  jav a 2  s  . c  o m
 * Created 2007/08/15
 * Copyright (C) 2003-2009  Naoki Iwami (naoki@limy.org)
 *
 * This file is part of Limy Eclipse Plugin.
 *
 * Limy Eclipse Plugin 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.
 *
 * Limy Eclipse Plugin 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 Limy Eclipse Plugin.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {

    public static void copyFile(File src, File dest) throws IOException {
        FileInputStream in = new FileInputStream(src);
        try {
            byte[] bs = getContentBytes(in);
            saveFile(dest, bs);
        } finally {
            in.close();
        }

    }

    public static byte[] getContentBytes(InputStream stream)
            throws IOException {
        BufferedInputStream in = null;
        if (stream instanceof BufferedInputStream) {
            in = (BufferedInputStream) stream;
        } else {
            in = new BufferedInputStream(stream);
        }
        try {
            int size = in.available();
            byte[] buff = new byte[size];
            int pos = 0;
            while (pos < size) {
                int realSize = in.read(buff, pos, size - pos);
                pos += realSize;
            }
            return buff;
        } finally {
            in.close();
        }
    }

    public static void saveFile(File targetFile, byte[] contents)
            throws IOException {
        FileOutputStream output = null;
        try {
            File parent = new File(targetFile.getAbsolutePath())
                    .getParentFile();
            if (parent != null) {
                parent.mkdirs();
            }
            output = new FileOutputStream(targetFile);
            output.write(contents);
        } finally {
            if (output != null) {
                output.close();
            }
        }
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File sourceFile, File targetFile)
  3. copyFile(File sourceFile, File targetFile)
  4. copyFile(File sourceFile, File targetFile)
  5. copyFile(File src, File dest)
  6. copyFile(File src, File dest)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dst)
  9. copyFile(File src, File dst)