Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static void copyFiles(String sourcePath, String targetPath) throws IOException {
        File srcDir = new File(sourcePath);
        File[] files = srcDir.listFiles();
        FileChannel in = null;
        FileChannel out = null;
        for (File file : files) {
            try {
                in = new FileInputStream(file).getChannel();
                File outDir = new File(targetPath);
                if (!outDir.exists()) {
                    outDir.mkdirs();
                }
                File outFile = new File(targetPath + File.separator + file.getName());
                out = new FileOutputStream(outFile).getChannel();
                in.transferTo(0, in.size(), out);
            } finally {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
            }
        }
    }
}