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;

public class Main {

    public static void copyDir(String src, String dst) {
        try {
            File fileSrc = new File(src);
            if (!fileSrc.exists()) {
                return;
            }
            File[] filelist = fileSrc.listFiles();
            File fileDst = new File(dst);
            if (!fileDst.exists()) {
                fileDst.mkdirs();
            }
            for (File f : filelist) {
                if (f.isDirectory()) {
                    copyDir(f.getPath() + "/", dst + f.getName() + "/");
                } else {
                    copyFile(f.getPath(), dst + f.getName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(String src, String dst) {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
            byte[] b = new byte[1024 * 5];
            int len = 0;
            while ((len = in.read(b)) > 0) {
                out.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}