Java FileInputStream Copy copyFile(String srFile, String dtFile)

Here you can find the source of copyFile(String srFile, String dtFile)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(String srFile, String dtFile) throws IOException 

Method Source Code

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

import java.io.File;

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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyFile(String srFile, String dtFile) throws IOException {
        try {/*from   w w w.  j a v  a2s  . c o m*/
            File f1 = new File(srFile);
            File f2 = new File(dtFile);
            copyFile(f1, f2);
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage() + " in the specified directory.");
            System.exit(0);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void copyFile(File srFile, File dtFile) throws IOException {
        try {

            InputStream in = new FileInputStream(srFile);

            // For Append the file.
            // OutputStream out = new FileOutputStream(f2,true);

            // For Overwrite the file.
            OutputStream out = new FileOutputStream(dtFile);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
            System.out.println("File copied.");
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage() + " in the specified directory.");
            System.exit(0);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

Related

  1. copyFile(String srcFilename, String dtsFilename)
  2. copyFile(String srcFilePath, String destFilePath)
  3. copyFile(String srcPath, String dstPath)
  4. copyFile(String srcPath, String dstPath, boolean replace)
  5. copyFile(String srFile, String dtFile)
  6. copyFile(String srFile, String dtFile)
  7. copyFile(String srFilePath, String dtFilePath)
  8. copyFile(String target, String source)
  9. copyFileAndReName(String destPath, String srcPath, String srcFile, String destFile)