Java FileReader Copy copyFile(String fromFilePath, String toFileDir)

Here you can find the source of copyFile(String fromFilePath, String toFileDir)

Description

Copies a file, located at fromFilePath into toFileDir.

License

Open Source License

Parameter

Parameter Description
fromFilePath a parameter
toFileDir a parameter

Declaration

public static String copyFile(String fromFilePath, String toFileDir) 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    /**//ww  w.j  a va 2  s.  c o  m
     * Copies a file, located at fromFilePath into toFileDir. 
     * File name remains the same.
     * @param fromFilePath
     * @param toFileDir
     */
    public static String copyFile(String fromFilePath, String toFileDir) {
        FileReader inputStream = null;
        FileWriter outputStream = null;

        String copiedFileName = (new File(fromFilePath)).getName();
        String toFilePath = toFileDir + "/" + copiedFileName;

        try {
            inputStream = new FileReader(fromFilePath);
            outputStream = new FileWriter(toFilePath);

            int c;
            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
            return toFilePath;
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

Related

  1. copyFile(File inputFile, File outputFile)
  2. copyFile(File source, File destination)
  3. copyFile(String input, String output)
  4. copyFile(String inputName, String destination)
  5. copyFile(String sourceFileName, String targetFileName)
  6. copyFile(String srcFileName, String dstFileName)