Java BufferedInputStream Copy copyFile(String src, String dest)

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

Description

copy File

License

Open Source License

Declaration

public static boolean copyFile(String src, String dest)
            throws IOException 

Method Source Code

//package com.java2s;
/* MarcWorx MARC Library - Utilities for manipulation of MARC records
 Copyright (C) 2013  Todd Walker, Talwood Solutions

 This program 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.//from ww  w. j  a va2s .c  o m

 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

public class Main {
    public static boolean copyFile(String src, String dest)
            throws IOException {
        return copyFile(new File(src), new File(dest));
    }

    public static boolean copyFile(File srcFile, File destFile)
            throws IOException {
        boolean result = true;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        // First let's see if the directory specified in dest exists, if not
        // create it
        File pFile = destFile.getParentFile();

        if (pFile != null && !pFile.exists()) {
            pFile.mkdirs();
        }
        // now copy the file over
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] buffer = new byte[32768];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (FileNotFoundException ex) {
            result = false;
        } finally {
            closeNoThrow(bis);
            closeNoThrow(bos);
            closeNoThrow(fis); // make sure these close - don't count on bis close
            closeNoThrow(fos); // make sure these clsoe - don't count on bos close
        }
        return result;
    }

    public static void closeNoThrow(InputStream s) {
        try {
            if (s != null) {
                s.close();
            }
        } catch (Exception ex) {
        }
    }

    public static void closeNoThrow(RandomAccessFile s) {
        try {
            if (s != null) {
                s.close();
            }
        } catch (Exception ex) {
        }
    }

    public static void closeNoThrow(OutputStream s) {
        try {
            if (s != null) {
                s.close();
            }
        } catch (Exception ex) {
        }
    }

    public static void closeNoThrow(Reader reader) {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (Exception ex) {
        }
    }

    public static void closeNoThrow(Writer writer) {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (Exception ex) {
        }
    }
}

Related

  1. copyFile(String fromFile, String toFile)
  2. copyFile(String source, String dest)
  3. copyFile(String source, String destination)
  4. copyFile(String source, String destination)
  5. copyFile(String sourceFilePath, String destinationFilePath)
  6. copyFileBytes(String srcFileName, String tarFileName)
  7. copyFileFromStream(InputStream in, File dest)
  8. copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir)
  9. copyFileNormal(File copyFrom, File copyTo)