Changeset 13676


Ignore:
Timestamp:
Oct 4, 2019 4:37:07 PM (2 months ago)
Author:
molsson
Message:

#5843 Added some methods to FileUtil.java.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/dev-mo-2265/Compiler/ModelicaFrontEnd/src/java/org/jmodelica/util/files/FileUtil.java

    r11394 r13676  
    33import java.io.File;
    44import java.io.IOException;
     5import java.nio.file.DirectoryStream;
    56import java.nio.file.Files;
     7import java.nio.file.Path;
    68import java.nio.file.Paths;
    79import java.nio.file.StandardCopyOption;
     
    155157        return names;
    156158    }
     159   
     160    /**
     161     * @return A collection of all files in {@code dir} matching the {@code glob}
     162     *         pattern
     163     */
     164    public static Collection<Path> getMatching(Path dir, String glob) throws IOException {
     165        Collection<Path> res = new ArrayList<>();
     166        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, glob)) {
     167            for (Path path : stream) {
     168                res.add(path);
     169            }
     170        }
     171        return res;
     172    }
    157173
    158174    /**
     
    234250     *             if there was any error copying the files.
    235251     */
    236     public static void copyRecursive(Collection<File> files, File destination, boolean replace) throws IOException {
     252    public static void copyRecursive(Iterable<File> files, File destination, boolean replace) throws IOException {
    237253        for (File file : files) {
    238254            copyRecursive(file, destination, replace);
     255        }
     256    }
     257   
     258    /**
     259     * Copies file to a directory. Contents of directories is copied recursively.
     260     *
     261     * @param sourceFile
     262     *            The file to copy.
     263     * @param destDir
     264     *            The destination directory.
     265     * @throws IOException
     266     *             if there was any error copying the files.
     267     */
     268    public static void copyRecursive(Path sourceFile, Path destDir) throws IOException {
     269        Path outFile = destDir.resolve(sourceFile.getFileName());
     270        if (Files.isDirectory(sourceFile)) {
     271            if (!Files.exists(outFile)) {
     272                Files.createDirectory(outFile);
     273            }
     274            try (DirectoryStream<Path> stream = Files.newDirectoryStream(sourceFile)) {
     275                copyRecursive(stream, outFile);
     276            }
     277        } else {
     278            Files.copy(sourceFile, outFile, StandardCopyOption.REPLACE_EXISTING);
     279        }
     280    }
     281   
     282    /**
     283     * Copies several files to a directory.
     284     *
     285     * @param sourceFiles
     286     *            The files to copy.
     287     * @param destDir
     288     *            The destination directory.
     289     * @throws IOException
     290     *             if there was any error copying the files.
     291     */
     292    public static void copyRecursive(Iterable<Path> sourceFiles, Path destDir) throws IOException {
     293        for (Path file : sourceFiles) {
     294            copyRecursive(file, destDir);
    239295        }
    240296    }
Note: See TracChangeset for help on using the changeset viewer.