Java FileReader Copy copyFile(File source, File destination)

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

Description

Copy a file in the File system.

License

Open Source License

Parameter

Parameter Description
source the source file
destination the destination file to save the content to

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File source, File destination) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
*
* This file is part of JMad.//from   w w  w  . ja va  2s  .  c  o m
* 
* Copyright (c) 2008-2011, CERN. 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
*
* 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.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    /**
     * Copy a file in the File system. Uses {@link FileReader} and {@link FileWriter} to copy the files content.
     * 
     * @param source the source file
     * @param destination the destination file to save the content to
     * @throws IOException
     */
    public static void copyFile(File source, File destination) throws IOException {
        FileReader inFile = new FileReader(source);
        FileWriter outFile = new FileWriter(destination);
        int content;

        while ((content = inFile.read()) != -1) {
            outFile.write(content);
        }

        inFile.close();
        outFile.close();
    }
}

Related

  1. copyFile(File inputFile, File outputFile)
  2. copyFile(String fromFilePath, String toFileDir)
  3. copyFile(String input, String output)
  4. copyFile(String inputName, String destination)
  5. copyFile(String sourceFileName, String targetFileName)