Java FileInputStream Copy copyFile(File sourceDir, File destDir, String filename)

Here you can find the source of copyFile(File sourceDir, File destDir, String filename)

Description

Copy a file from one directory to another.

License

Open Source License

Parameter

Parameter Description
sourceDir The directory where the file starts
destDir The directory where we make a copy
filename The file's name

Exception

Parameter Description

Declaration

public static void copyFile(File sourceDir, File destDir, String filename) throws java.io.IOException 

Method Source Code


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

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

public class Main {
    private static int BUFFERSIZE = 16 * 1024;

    /**//from   w w  w  .j  a v a 2  s  . c o m
    Copy a file from one directory to another. <br>
    This copies a file, without renaming it.  The source file is not deleted.
    @param sourceDir The directory where the file starts
    @param destDir The directory where we make a copy
    @param filename The file's name
        
    @throws java.io.IOException on IO error
    */
    public static void copyFile(File sourceDir, File destDir, String filename) throws java.io.IOException {
        copyFile(sourceDir, destDir, filename, filename);
    }

    /**
    Copy a file within a directory. <br>
    This copies a file.  The source file is not deleted.
    @param dir The directory where the files are
    @param srcFile The source file's name
    @param destFile The destination file's name
        
    @throws java.io.IOException on IO error
    */
    public static void copyFile(File dir, String srcFile, String destFile) throws java.io.IOException {
        copyFile(dir, dir, srcFile, destFile);
    }

    /**
    Copy a file. <br>
    This copies a file.  The source file is not deleted.
    @param sourceDir The directory where the file starts
    @param destDir The directory where we make a copy
    @param srcFile The source file's name
    @param destFile The destination file's name
        
    @throws java.io.IOException on IO error
    */
    public static void copyFile(File sourceDir, File destDir, String srcFile, String destFile)
            throws java.io.IOException {
        File inf = new File(sourceDir, srcFile);
        File outf = new File(destDir, destFile);

        // open the files, copy them by buffer
        FileInputStream fis = new FileInputStream(inf);
        FileOutputStream fos = new FileOutputStream(outf);
        copyStream(fis, fos);
        fis.close();
        fos.close();

        return;
    }

    public static void copyStream(InputStream input, OutputStream output) throws IOException {
        byte[] buffer = new byte[BUFFERSIZE];
        int nRead = 0;
        //int total = 0;
        while (true) {
            nRead = input.read(buffer, 0, BUFFERSIZE);
            if (nRead < 0) {
                break;
            }
            output.write(buffer, 0, nRead);
            //total += nRead;
        }
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File source, File target)
  3. copyFile(File source, File target, boolean createParents, FileFilter filter)
  4. copyFile(File source, File target, boolean deleteSourceAfter)
  5. copyFile(File source, File target, boolean replaceIfExists)
  6. copyFile(File sourceFile, File destDir)
  7. copyFile(File sourceFile, File destFile)
  8. copyFile(File sourceFile, File destFile)
  9. copyFile(File sourceFile, File destFile)