All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marklogic.hub.util.FileUtil Maven / Gradle / Ivy

There is a newer version: 6.1.1
Show newest version
/*
 * Copyright (c) 2021 MarkLogic Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.marklogic.hub.util;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileUtil {

    public static void copy(InputStream source, File destination) {
        try {
            FileUtils.copyInputStreamToFile(source, destination);
        }
        catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static List listDirectFolders(File rootDirectory) {
        List folders = new ArrayList<>();
        if (rootDirectory.exists() && rootDirectory.isDirectory()) {
            File[] files = rootDirectory.listFiles();
            if (files == null) {
                return folders;
            }
            for (File file : files) {
                if (file.isDirectory() && !file.isHidden()) {
                    folders.add(file.getName());
                }
            }
        }
        Collections.sort(folders);
        return folders;
    }
    public static List listDirectFolders(String rootDirectoryName) {
        return listDirectFolders(new File(rootDirectoryName));
    }
    public static List listDirectFolders(Path rootDirectoryPath) {
        return listDirectFolders(rootDirectoryPath.toFile());
    }

    public static List listDirectFiles(File rootDirectory) {
        List filenames = new ArrayList<>();
        if (rootDirectory.exists() && rootDirectory.isDirectory()) {
            File[] files = rootDirectory.listFiles();
            if (files == null) {
                return filenames;
            }
            for (File file : files) {
                if (!file.isDirectory() && !file.isHidden()) {
                    filenames.add(file.getName());
                }
            }
        }
        Collections.sort(filenames);
        return filenames;
    }
    public static List listDirectFiles(String rootDirectoryName) {
        return listDirectFiles(new File(rootDirectoryName));
    }
    public static List listDirectFiles(Path rootDirectoryPath) {
        return listDirectFiles(rootDirectoryPath.toFile());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy