Java FileInputStream Copy copyFile(String target, String source)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(String target, String source) 

Method Source Code

//package com.java2s;
/**/*from   w  w w.  jav  a2s .c om*/
* This file is part of dev.utils.
*
* dev.utils 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.
*
* You should have received a copy of the GNU General Public License
* along with dev.utils.  If not, see <http://www.gnu.org/licenses/>.
*
* File      : FileHelper.java
* Created   : November 28, 2006, 5:13 PM
*/

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    public static void copyFile(String target, String source) {
        byte[] bytes = getFileInBytes(source);
        try {
            writeBytes(bytes, target);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *   @param fileName full name of the file
     */
    public static byte[] getFileInBytes(String fileName) {
        return getFileInBytes(new File(fileName));
    }

    /**
     *  modified for buffering, but ot tested...
     */
    public static byte[] getFileInBytes(File fileName) {
        byte bytes[] = null;
        try {
            FileInputStream fs = new FileInputStream(fileName);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            int b;
            byte[] byteArr = new byte[1024];
            while ((b = fs.read(byteArr)) != -1) {
                bos.write(byteArr, 0, b);
            }

            bytes = bos.toByteArray();

            fs.close();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bytes;
    }

    public static void writeBytes(byte[] bytes, String file) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bytes);
        fos.close();
    }
}

Related

  1. copyFile(String srcPath, String dstPath, boolean replace)
  2. copyFile(String srFile, String dtFile)
  3. copyFile(String srFile, String dtFile)
  4. copyFile(String srFile, String dtFile)
  5. copyFile(String srFilePath, String dtFilePath)
  6. copyFileAndReName(String destPath, String srcPath, String srcFile, String destFile)
  7. copyFileAsStream(String srcFilePathName, String dstFilePathName)
  8. copyFileBin(File sourceFile, File targetFile)
  9. copyFileBinary(String source, String destination)