Java FileChannel Copy copyFileToDirectory(String fileFrom, String destinationDirectory)

Here you can find the source of copyFileToDirectory(String fileFrom, String destinationDirectory)

Description

Obtains the file name from the fileIn parameter Copies the file to the destination directory using the same file name

License

Open Source License

Parameter

Parameter Description
fileFrom a parameter
destinationDirectory a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFileToDirectory(String fileFrom, String destinationDirectory) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  ww w  .  j  a  v a  2  s  .c  om*/
 * nz.govt.natlib.ndha.common.Common - Software License
 *
 * Copyright 2007/2008 National Library of New Zealand.
 * All rights reserved.
 *
 * 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 
 *
 * or the file "LICENSE.txt" included with the software.
 *
 * 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.
 *
 * FileUtils.java
 * $Rev$
 * PlayerM
 * 26/11/2007
 *
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
    /**
     * Obtains the file name from the fileIn parameter
     * Copies the file to the destination directory using the same file name
     *
     * @param fileFrom
     * @param destinationDirectory
     * @throws IOException
     */
    public static void copyFileToDirectory(String fileFrom, String destinationDirectory) throws IOException {
        File inFile = new File(fileFrom);
        String fileName = inFile.getName();
        copyFile(fileFrom, destinationDirectory + "/" + fileName);
    }

    /**
     * Copies a file from one place to another
     *
     * @param fileFrom full file name
     * @param fileTo   full file name
     * @throws IOException
     */
    public static void copyFile(String fileFrom, String fileTo) throws IOException {
        File in = new File(fileFrom);
        File out = new File(fileTo);
        copyFile(in, out);
    }

    /**
     * Name is self explanatory
     *
     * @param from
     * @param to
     * @throws IOException
     */
    public static void copyFile(File from, File to) throws IOException {
        FileChannel inChannel = new FileInputStream(from).getChannel();
        FileChannel outChannel = new FileOutputStream(to).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            throw e;
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }
}

Related

  1. copyFiles(File originalFile, File destinationFile)
  2. copyFiles(File source, File dest)
  3. copyFiles(File srcFile, File dstFile, boolean overwrite)
  4. copyFiles(final File fromFile, final File toFile)
  5. copyFileToDir(File file, File destDir)
  6. copyFileToFolder(final String resultFile, final String targetPath)
  7. copyFileToStream(File in, OutputStream out)
  8. copyFileUsingChannel(File source, File dest)
  9. copyFileUsingChannel(File source, File dest)