Java BufferedInputStream Copy copyFile(final File src, final File dest)

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

Description

copy File

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
/*// w ww.  j a v  a2s. c  o m
 * JBoss, Home of Professional Open Source.
 * Copyright 2012 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
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(final File src, final File dest) throws IOException {
        final InputStream in = new BufferedInputStream(new FileInputStream(src));
        try {
            copyFile(in, dest);
        } finally {
            close(in);
        }
    }

    public static void copyFile(final InputStream in, final File dest) throws IOException {
        dest.getParentFile().mkdirs();
        final OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
        try {
            int i = in.read();
            while (i != -1) {
                out.write(i);
                i = in.read();
            }
        } finally {
            close(out);
        }
    }

    public static void close(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException ignore) {
        }
    }
}

Related

  1. copyFile(final File fSource, final File fDest)
  2. copyFile(final File source, final File dest, final boolean overwrite)
  3. copyFile(final File source, final File destination)
  4. copyFile(final File source, final File destination, final boolean overwrite)
  5. copyFile(final File sourceFile, final File targetFile)
  6. copyFile(final File to, final File from)
  7. copyFile(InputStream is, File newFile)
  8. copyFile(InputStream src, File dest)
  9. copyFile(InputStream src, File dst)