Java FileReader Copy copyFile(String sourceFileName, String targetFileName)

Here you can find the source of copyFile(String sourceFileName, String targetFileName)

Description

Copies a file to the given target.

License

Open Source License

Parameter

Parameter Description
sourceFileName file name of the file to be copied
targetFileName file name where to copy the file

Declaration

public static void copyFile(String sourceFileName, String targetFileName) 

Method Source Code


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

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    /**/*from  w w w.  ja  v  a  2  s. co  m*/
     * Copies a file to the given target.
     *
     * @param sourceFileName
     *            file name of the file to be copied
     * @param targetFileName
     *            file name where to copy the file
     */
    public static void copyFile(String sourceFileName, String targetFileName) {
        final File inputFile = new File(sourceFileName);
        final File outputFile = new File(targetFileName);

        try {
            final FileReader in = new FileReader(inputFile);
            final FileWriter out = new FileWriter(outputFile);
            int c;
            while ((c = in.read()) != -1) {
                out.write(c);
            }
            in.close();
            out.close();
        } catch (final FileNotFoundException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyFile(File inputFile, File outputFile)
  2. copyFile(File source, File destination)
  3. copyFile(String fromFilePath, String toFileDir)
  4. copyFile(String input, String output)
  5. copyFile(String inputName, String destination)
  6. copyFile(String srcFileName, String dstFileName)
  7. copyFileToDir(File inputFile, File outputDir)