Java BufferedInputStream 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;
/*// ww w.  j  ava 2 s.  c o  m
** Tim Endres' utilities package.
** Copyright (c) 1997 by Tim Endres
** 
** This program is free software.
** 
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included   with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE. 
** 
*/

import java.io.*;

public class Main {
    public static void copyFile(File from, File to) throws IOException {
        int bytes;
        long length;
        long fileSize;

        BufferedInputStream in = null;
        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new FileInputStream(from));
        } catch (IOException ex) {
            throw new IOException(
                    "FileUtilities.copyFile: opening input stream '" + from.getPath() + "', " + ex.getMessage());
        }

        try {
            out = new BufferedOutputStream(new FileOutputStream(to));
        } catch (Exception ex) {
            try {
                in.close();
            } catch (IOException ex1) {
            }
            throw new IOException(
                    "FileUtilities.copyFile: opening output stream '" + to.getPath() + "', " + ex.getMessage());
        }

        byte[] buffer;
        buffer = new byte[8192];
        fileSize = from.length();

        for (length = fileSize; length > 0;) {
            bytes = (int) (length > 8192 ? 8192 : length);

            try {
                bytes = in.read(buffer, 0, bytes);
            } catch (IOException ex) {
                try {
                    in.close();
                    out.close();
                } catch (IOException ex1) {
                }
                throw new IOException("FileUtilities.copyFile: reading input stream, " + ex.getMessage());
            }

            if (bytes < 0)
                break;

            length -= bytes;

            try {
                out.write(buffer, 0, bytes);
            } catch (IOException ex) {
                try {
                    in.close();
                    out.close();
                } catch (IOException ex1) {
                }
                throw new IOException("FileUtilities.copyFile: writing output stream, " + ex.getMessage());
            }
        }

        try {
            in.close();
            out.close();
        } catch (IOException ex) {
            throw new IOException("FileUtilities.copyFile: closing file streams, " + ex.getMessage());
        }
    }
}

Related

  1. copyFile(File file, File ziel)
  2. copyFile(File from, File to)
  3. copyFile(File from, File to)
  4. copyFile(File from, File to)
  5. copyFile(File fromFile, File toFile)