Java FileChannel Copy copyFile(String input, String output)

Here you can find the source of copyFile(String input, String output)

Description

copies a file from input to output

License

Open Source License

Parameter

Parameter Description
input - the file to be copied
output - the destination file

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(String input, String output)
        throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;
import java.nio.channels.FileChannel;

public class Main {
    /**/*from  ww w .ja  v a 2  s  . co m*/
     * copies a file from input to output
     * 
     * @param input
     *            - the file to be copied
     * @param output
     *            - the destination file
     * @throws IOException
     */
    public static void copyFile(String input, String output)
            throws IOException {
        // Open the file to be copied
        BufferedReader in = new BufferedReader(new InputStreamReader(
                new FileInputStream(input)));
        // And the one where to copy it to
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(output)));

        String line = null;
        while ((line = in.readLine()) != null) {

            out.write(line);
            out.newLine();
        }

        // close the streams

        in.close();
        out.close();
    }

    public static void copyFile(File sourceFile, File destFile)
            throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();

            // previous code: destination.transferFrom(source, 0,
            // source.size());
            // to avoid infinite loops, should be:
            long count = 0;
            long size = source.size();
            while ((count += destination.transferFrom(source, count, size
                    - count)) < size)
                ;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
}

Related

  1. copyFile(String fromPath, String toPath)
  2. copyFile(String in, String out)
  3. copyFile(String infile, String outfile)
  4. copyFile(String inFile, String outFile)
  5. copyFile(String inName, String otName)
  6. copyFile(String origPath, String destPath)
  7. copyFile(String source, String destination)
  8. copyFile(String source, String destination)
  9. copyFile(String source, String destination)