FileUtil.java 1.11 KB
Newer Older
Dominik Vayhinger's avatar
Dominik Vayhinger committed
1
2
3
package de.hftstuttgart.utils;

import java.io.File;
4
5
6
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Dominik Vayhinger's avatar
Dominik Vayhinger committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

/**
 * Helper Class for all file related tasks.
 *
 * Created by Marcel Bochtler on 05.01.17.
 */
public class FileUtil {

    /**
     * Delete the folder and all containing files.
     * @param folder    Folder to delete
     */
    public static void deleteFolderRecursively(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolderRecursively(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }

33
34
35
36
37
38
39
40
41
42
    public static void copyFolder(Path src, Path dst) throws IOException {
        Files.walk(src)
            .forEach(source -> {
                try {
                    Files.copy(source, dst.resolve(src.relativize(source)));
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage(), e);
                }
            });
    }
Dominik Vayhinger's avatar
Dominik Vayhinger committed
43
}