Java File Copy nio copyFile(final File source, final File target)

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

Description

copy File

License

Open Source License

Parameter

Parameter Description
source a parameter
target a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(final File source, final File target) throws IOException 

Method Source Code

//package com.java2s;
/*/*  w  ww . j  ava  2 s.  c o m*/
 *
 *  * Copyright (c) 2014- MHISoft LLC and/or its affiliates. All rights reserved.
 *  * Licensed to MHISoft LLC under one or more contributor
 *  * license agreements. See the NOTICE file distributed with
 *  * this work for additional information regarding copyright
 *  * ownership. MHISoft LLC licenses this file to you 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.Closeable;

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

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    private static final int BUFFER = 4096 * 16;

    /**
     * @param source
     * @param target
     * @throws IOException
     */
    public static void copyFile(final File source, final File target) throws IOException {
        FileChannel in = null;
        FileChannel out = null;
        //   long totalFileSize = 0;

        try {
            in = new FileInputStream(source).getChannel();
            out = new FileOutputStream(target).getChannel();
            //totalFileSize = in.size();

            ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER);
            int readSize = in.read(buffer);
            long totalRead = 0;
            //int progress = 0;

            //long startTime, endTime  ;

            while (readSize != -1) {

                //startTime = System.currentTimeMillis();
                totalRead = totalRead + readSize;

                //progress = (int) (totalRead * 100 / totalFileSize);

                buffer.flip();

                while (buffer.hasRemaining()) {
                    out.write(buffer);
                    //System.out.printf(".");
                    //showPercent(rdProUI, totalSize/size );
                }
                buffer.clear();
                readSize = in.read(buffer);

                //endTime = System.currentTimeMillis();
            }

        } finally {
            close(in);
            close(out);

        }
    }

    private static void close(Closeable closable) {
        if (closable != null) {
            try {
                closable.close();
            } catch (IOException e) {
                //
            }
        }
    }
}

Related

  1. copyFile(File source, File dest, boolean visibleFilesOnly)
  2. copyFile(File src, File dest)
  3. copyFile(FileChannel srcFc, File dstFile)
  4. copyFile(final File source, final File dest)
  5. copyFile(final File source, final File target)
  6. copyFileByMapped(String sourcePath, String targetPath)
  7. copyToFile(final InputStream is, final File file)
  8. copyToTempFile(final InputStream is, final String prefix, final String suffix)
  9. copyToTmpFile(InputStream in, String prefix, String suffix)