Java FileInputStream Copy copyFile(String source, String targetDirectory, String filesep)

Here you can find the source of copyFile(String source, String targetDirectory, String filesep)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(String source, String targetDirectory, String filesep) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void copyFile(String source, String targetDirectory, String filesep) throws IOException {

        byte[] buffer = new byte[4096];
        int bytes_read;

        FileInputStream fis = null;
        FileOutputStream fos = null;
        File fileToCopy = null;/*from  ww  w. j a  v a  2s . co m*/
        File copiedFile = null;

        try {
            fileToCopy = new File(System.getProperty("user.dir") + filesep + source);
            copiedFile = new File(System.getProperty("user.dir") + filesep + targetDirectory + filesep + source);

            fis = new FileInputStream(fileToCopy);
            fos = new FileOutputStream(copiedFile);
            while ((bytes_read = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytes_read);
            } //end while
            fos.flush();
            fos.close();
        } //end try
        catch (java.io.IOException ioe) {
            System.err.println("An error has occurred while copying file:\n" + ioe);
            ioe.printStackTrace();
            return;
        }
    }
}

Related

  1. copyFile(String source, String destination)
  2. copyFile(String source, String destination)
  3. copyFile(String source, String destination)
  4. copyFile(String source, String destination)
  5. copyFile(String source, String target)
  6. copyFile(String sourceFile, String destDir, String newFileName)
  7. copyFile(String sourceFile, String destFile)
  8. copyFile(String sourcefile, String targetfile)
  9. copyFile(String sourcefile, String targetFile)