Java FileInputStream Copy copyFile(String srcFile, String dstFile)

Here you can find the source of copyFile(String srcFile, String dstFile)

Description

Copy the contents of the first file to the second file.

License

Academic Free License

Parameter

Parameter Description
srcFile a parameter
dstFile a parameter

Declaration

public static void copyFile(String srcFile, String dstFile) 

Method Source Code

//package com.java2s;
//License from project: Academic Free License 

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class Main {
    /**/*from   w w  w  .  j av a  2 s .  co m*/
     * Copy the contents of the first file to the second file.
     * 
     * @param srcFile
     * @param dstFile
     */
    public static void copyFile(String srcFile, String dstFile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        byte ba[] = new byte[10240];
        int numRead = 0;
        try {
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(dstFile);

            numRead = fis.read(ba);
            while (numRead > 0) {
                fos.write(ba, 0, numRead);
                numRead = fis.read(ba);
            }

            fos.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            close(fis);
            close(fos);
        }
    }

    /**
     * Close the output stream and trap any exceptions.
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ex) {
            }
        }
    }

    public static void close(Writer fw) {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception ex) {
            }
            ;
        }
    }

    /**
     * Close the input stream and trap any exceptions.
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ex) {
            }
        }
    }

    /**
     * Close the reader.
     * 
     * @param r
     */
    public static void close(Reader r) {
        if (r != null) {
            try {
                r.close();
            } catch (Exception ex) {

            }
        }
    }
}

Related

  1. copyFile(String src, String tar)
  2. copyFile(String src, String to)
  3. copyFile(String srcDir, String destDir)
  4. copyFile(String srcFile, String destFile)
  5. copyFile(String srcFile, String dstFile)
  6. copyFile(String srcFileName, String destFileName)
  7. copyFile(String srcFilename, String dtsFilename)
  8. copyFile(String srcFilePath, String destFilePath)
  9. copyFile(String srcPath, String dstPath)