Java FileReader Copy copyFile(String inputName, String destination)

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

Description

Copyies a file to a specified desitination

License

Open Source License

Parameter

Parameter Description
inputName File
destination Filename

Exception

Parameter Description
FileNotFoundException if no input file exists
IOException if cannot read or write

Declaration

public static void copyFile(String inputName, String destination) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*/* w  ww.  j  a v  a  2 s. c om*/
StatCvs - CVS statistics generation 
Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
http://statcvs.sf.net/
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
   $RCSfile$ 
   Created on $Date$ 
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * Copyies a file to a specified desitination
     * @param inputName File
     * @param destination Filename
     * @throws FileNotFoundException if no input file exists
     * @throws IOException if cannot read or write
     */
    public static void copyFile(String inputName, String destination) throws FileNotFoundException, IOException {
        File input = new File(inputName);
        File outputFile = new File(destination);
        FileReader in = new FileReader(input);
        FileWriter out = new FileWriter(outputFile);
        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        in.close();
        out.close();
    }

    /**
     * Copy a InputStream into a File
     * @param in source
     * @param out destination
     * @throws FileNotFoundException if not found
     * @throws IOException if read/write error
     */
    public static void copyFile(InputStream in, File out) throws FileNotFoundException, IOException {

        InputStream fis = in;
        FileOutputStream fos = new FileOutputStream(out);
        byte[] buf = new byte[1024];
        int i = 0;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fis.close();
        fos.close();
    }
}

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 sourceFileName, String targetFileName)
  6. copyFile(String srcFileName, String dstFileName)
  7. copyFileToDir(File inputFile, File outputDir)