Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

public class Main {
    public static void moveAll(final File fromFolder, final File toFolder) {
        moveAll(fromFolder, toFolder, false, false);
    }

    public static void moveAll(final File fromFolder, final File toFolder, final boolean overwrite,
            final boolean clearDestinationFolder) {
        if (!fromFolder.exists())
            throw new RuntimeException("From folder " + fromFolder.getAbsolutePath() + " does not exist");
        if (!toFolder.exists())
            toFolder.mkdirs();
        else if (clearDestinationFolder)
            recursiveDelete(toFolder, true);
        for (File fromFile : fromFolder.listFiles()) {
            File toFile = new File(toFolder, fromFile.getName());
            if (!clearDestinationFolder && toFile.exists()) {
                if (overwrite)
                    recursiveDelete(toFile);
                else
                    continue;
            }
            if (!fromFile.renameTo(toFile))
                throw new RuntimeException(
                        "Cannot rename " + fromFile.getAbsolutePath() + " to " + toFile.getAbsolutePath());
        }
    }

    public static void recursiveDelete(final File file) {
        recursiveDelete(file, false);
    }

    public static void recursiveDelete(final File file, final boolean onlyDeleteChildren) {
        if (!file.exists())
            return;
        if (file.isDirectory()) {
            for (File child : file.listFiles()) {
                recursiveDelete(child, false);
            }
        }
        if (!onlyDeleteChildren && !file.delete())
            throw new RuntimeException("Cannot delete file " + file.getAbsolutePath());
    }
}