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

com.cybermkd.common.util.stream.Filer Maven / Gradle / Ivy

package com.cybermkd.common.util.stream;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Created by ice on 15-1-28.
 */
public class Filer {

    public static File mkDirs(String path) {
        return mkDirs(new File(path));
    }

    public static File mkDirs(File file) {
        if (file == null) {
            throw new FileException("File could not be null.");
        }
        File parent = file.getParentFile();
        if (!parent.exists()) {
            if (!parent.mkdirs()) {
                throw new FileException("Directory " + parent.getAbsolutePath() + " not exists and can not create directory.");
            }
        }
        return file;
    }

    public static boolean exist(String file) {
        Enumeration urls = null;
        try {
            urls = Thread.currentThread().getContextClassLoader().getResources(file);
            while (urls.hasMoreElements()) {
                return true;
            }
        } catch (IOException e) {
            throw new FileException("Could not getResource from file - " + file, e);
        }
        return false;
    }

    public static List files(String dir) {
        return files(new File(dir));
    }

    public static List files(File dir) {
        List result = new ArrayList();
        if (dir.exists()) {
            File[] files = dir.listFiles();
            if (files != null && files.length > 0) {
                for (File file : files) {
                    if (!file.isHidden()) {
                        if (file.isDirectory()) {
                            result.addAll(files(new File(file.getAbsolutePath())));
                        } else {
                            result.add(file);
                        }
                    }
                }
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy