Java FileChannel Copy fileCopy(String sourceFolder, String destinationFolder)

Here you can find the source of fileCopy(String sourceFolder, String destinationFolder)

Description

Copies all the files from the source folder to the destination folder.

License

Open Source License

Parameter

Parameter Description
sourceFolder a parameter
destinationFolder a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void fileCopy(String sourceFolder, String destinationFolder) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**/*from w w  w .ja  v a2 s  .c om*/
     * Copies all the files from the source folder to the destination folder.
     * 
     * @param sourceFolder
     * @param destinationFolder
     * @throws IOException
     */
    public static void fileCopy(String sourceFolder, String destinationFolder) throws IOException {
        File source = new File(sourceFolder);
        FileFilter filter = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                if (pathname.isFile()) {
                    if (pathname.getName().indexOf("jpg") != -1 || pathname.getName().indexOf("jpeg") != -1
                            || pathname.getName().indexOf("png") != -1)
                        return true;
                }

                return false;
            }
        };
        File[] files = source.listFiles(filter);
        for (File file : files) {
            FileChannel fIn = new FileInputStream(file).getChannel();
            FileChannel fOut = new FileOutputStream(new File(destinationFolder + file.getName())).getChannel();
            fIn.transferTo(0, fIn.size(), fOut);
        }
    }
}

Related

  1. doCopyDirectory(File srcDir, File destDir, boolean preserveFileDate, List exclusionList)
  2. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  3. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  4. fCopy(FileInputStream src, File dest)
  5. fileChannelCopy(File src, File dest)
  6. fileStreamCopy(FileInputStream in, FileOutputStream out, boolean closeOutput)
  7. nioCopy(File source, File target, FilenameFilter filter)
  8. nioCopy(FileOutputStream fos, FileInputStream fis)
  9. nioCopyFile(File source, File target, boolean replaceIfExists)