Example usage for java.io File list

List of usage examples for java.io File list

Introduction

In this page you can find the example usage for java.io File list.

Prototype

public String[] list() 

Source Link

Document

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

Usage

From source file:net.refractions.udig.catalog.wmsc.server.TileImageReadWriter.java

/**
 * Recursively deletes all subdirs and files of a directory and then deletes the given
 * directory. Returns false at the moment any attempts to delete fail.
 * // w w w  . ja  v a2s  .co  m
 * @param dir
 * @return true on success
 */
private static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    // The directory is now empty so delete it
    return dir.delete();
}

From source file:com.glaf.core.xml.XmlProperties.java

public static void reload() {
    if (!loading.get()) {
        InputStream inputStream = null;
        try {//from   w ww . j  a v a2s  .co  m
            loading.set(true);
            String config = SystemProperties.getConfigRootPath() + "/conf/templates/xml";
            File directory = new File(config);
            if (directory.exists() && directory.isDirectory()) {
                String[] filelist = directory.list();
                if (filelist != null) {
                    for (int i = 0, len = filelist.length; i < len; i++) {
                        String filename = config + "/" + filelist[i];
                        File file = new File(filename);
                        if (file.isFile() && file.getName().endsWith(".properties")) {
                            inputStream = new FileInputStream(file);
                            Properties p = PropertiesUtils.loadProperties(inputStream);
                            if (p != null) {
                                Enumeration<?> e = p.keys();
                                while (e.hasMoreElements()) {
                                    String key = (String) e.nextElement();
                                    String value = p.getProperty(key);
                                    properties.setProperty(key, value);
                                    properties.setProperty(key.toLowerCase(), value);
                                    properties.setProperty(key.toUpperCase(), value);
                                }
                            }
                            IOUtils.closeStream(inputStream);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            loading.set(false);
            IOUtils.closeStream(inputStream);
        }
    }
}

From source file:de.uzk.hki.da.utils.FolderUtils.java

/**
 * Compares two folders for equality. Compares the folderstructure and the
 * compares the bitwise equality of the included files.
 *
 * @param lhs/*  w w  w  .j  av a2 s.  com*/
 *            relative path to the first folder
 * @param rhs
 *            relative path to the second folder
 * @return true if lhs equals rhs
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean compareFolders(File lhs, File rhs) throws IOException {

    String rhsParentPath = rhs.getAbsolutePath();
    String lhsParentPath = lhs.getAbsolutePath();

    String lhsChildren[] = lhs.list();
    String rhsChildren[] = rhs.list();

    // Sometimes the order of the listings are not equal even though the
    // files contained are
    Arrays.sort(lhsChildren);
    Arrays.sort(rhsChildren);

    boolean filesAreEqual = true;
    for (int i = 0; i < lhsChildren.length; i++) {

        File lhsf = new File(lhsParentPath + "/" + lhsChildren[i]);
        File rhsf = new File(rhsParentPath + "/" + rhsChildren[i]);

        if (lhsf.isFile()) {

            if (!FileUtils.contentEquals(lhsf, rhsf))
                filesAreEqual = false;
        }

        if (lhsf.isDirectory()) {
            if (!compareFolders(lhsf, rhsf))
                filesAreEqual = false;
        }
    }
    return filesAreEqual;
}

From source file:com.glaf.shiro.SecurityConfig.java

public static void reload() {
    InputStream inputStream = null;
    try {//from  w  w w . j  a  v  a2 s .c om
        loading.set(true);
        String config_path = SystemProperties.getConfigRootPath() + "/conf/security/";
        String defaultFileName = config_path + "system-security.properties";
        File defaultFile = new File(defaultFileName);
        if (defaultFile.isFile()) {
            inputStream = new FileInputStream(defaultFile);
            LinkedHashMap<String, String> p = PropertiesUtils.load(inputStream);
            if (p != null) {
                Iterator<String> it = p.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    String value = p.get(key);
                    /**
                     * ?????????
                     */
                    if (!filterChainDefinitionMap.containsKey(key)) {
                        filterChainDefinitionMap.put(key, value);
                    }
                }
            }
            IOUtils.closeQuietly(inputStream);
            inputStream = null;
        }
        File directory = new File(config_path);
        if (directory.isDirectory()) {
            String[] filelist = directory.list();
            for (int i = 0; i < filelist.length; i++) {
                String filename = config_path + filelist[i];
                if (StringUtils.equals(filename, "system-security.properties")) {
                    continue;
                }
                File file = new File(filename);
                if (file.isFile() && file.getName().endsWith(".properties")) {
                    inputStream = new FileInputStream(file);
                    LinkedHashMap<String, String> p = PropertiesUtils.load(inputStream);
                    if (p != null) {
                        Iterator<String> it = p.keySet().iterator();
                        while (it.hasNext()) {
                            String key = it.next();
                            String value = p.get(key);
                            /**
                             * ?????????
                             */
                            if (!filterChainDefinitionMap.containsKey(key)) {
                                filterChainDefinitionMap.put(key, value);
                            }
                        }
                    }
                    IOUtils.closeQuietly(inputStream);
                    inputStream = null;
                }
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        loading.set(false);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:FileMonitor.java

/**
 * Copy the source file system structure into the supplied target location. If
 * the source is a file, the destiniation will be created as a file; if the
 * source is a directory, the destination will be created as a directory.
 * //from w  w w . j a v a2  s. c o m
 * @param sourceFileOrDirectory
 *          the file or directory whose contents are to be copied into the
 *          target location
 * @param destinationFileOrDirectory
 *          the location where the copy is to be placed; does not need to
 *          exist, but if it does its type must match that of <code>src</code>
 * @return the number of files (not directories) that were copied
 * @throws IllegalArgumentException
 *           if the <code>src</code> or <code>dest</code> references are
 *           null
 * @throws IOException
 */
public static int copy(File sourceFileOrDirectory, File destinationFileOrDirectory) throws IOException {
    int numberOfFilesCopied = 0;
    if (sourceFileOrDirectory.isDirectory()) {
        destinationFileOrDirectory.mkdirs();
        String list[] = sourceFileOrDirectory.list();

        for (int i = 0; i < list.length; i++) {
            String dest1 = destinationFileOrDirectory.getPath() + File.separator + list[i];
            String src1 = sourceFileOrDirectory.getPath() + File.separator + list[i];
            numberOfFilesCopied += copy(new File(src1), new File(dest1));
        }
    } else {
        InputStream fin = new FileInputStream(sourceFileOrDirectory);
        fin = new BufferedInputStream(fin);
        try {
            OutputStream fout = new FileOutputStream(destinationFileOrDirectory);
            fout = new BufferedOutputStream(fout);
            try {
                int c;
                while ((c = fin.read()) >= 0) {
                    fout.write(c);
                }
            } finally {
                fout.close();
            }
        } finally {
            fin.close();
        }
        numberOfFilesCopied++;
    }
    return numberOfFilesCopied;
}

From source file:com.glaf.template.config.TemplateConfig.java

public static void reload() {
    if (!loading.get()) {
        InputStream inputStream = null;
        try {/*from ww  w.ja va  2 s  . c  o m*/
            loading.set(true);
            String config = SystemProperties.getConfigRootPath() + "/conf/templates/";
            logger.debug("load config dir:" + config);
            File directory = new File(config);
            if (directory.exists() && directory.isDirectory()) {
                TemplateReader reader = new TemplateReader();
                String[] filelist = directory.list();
                if (filelist != null) {
                    for (int i = 0, len = filelist.length; i < len; i++) {
                        String filename = config + filelist[i];
                        File file = new File(filename);
                        if (file.isFile() && file.getName().endsWith(".xml")) {
                            logger.debug("read config:" + filename);
                            inputStream = new FileInputStream(file);
                            List<Template> templates = reader.readTemplates(inputStream);
                            if (templates != null && !templates.isEmpty()) {
                                for (Template template : templates) {
                                    if (template.getName() != null) {
                                        concurrentMap.put(template.getName().toLowerCase(), template);
                                    }
                                    if (template.getTemplateId() != null) {
                                        concurrentMap.put(template.getTemplateId().toLowerCase(), template);
                                    }
                                }
                            }
                            IOUtils.closeStream(inputStream);
                        }
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex);
        } finally {
            loading.set(false);
            IOUtils.closeStream(inputStream);
        }
    }
}

From source file:com.vmware.appfactory.file.FileHelper.java

/**
 * Check if a directory contains files in them.
 *
 * @param dirPath//from w w  w  .  j av  a2  s.  c  o  m
 * @return
 */
public static boolean filesExistInDir(String dirPath) {
    File file = new File(dirPath);
    if (file.isDirectory()) {
        return (file.list().length != 0);
    }
    return false;
}

From source file:eionet.gdem.utils.ZipUtil.java

/**
 * Function zips all the files in directory and subdirectories and adds these into zip file
 *
 * @param dir2zip/*from   w w w . j  a  v a2  s. com*/
 *            - directory that will be zipped
 * @param outZip
 *            - ZipOutputStream represents the zip file, where the files will be placed
 * @param sourceDir
 *            - root directory, where the zipping started
 * @throws IOException If an error occurs.
 */
public static void zipDir(String dir2zip, ZipOutputStream outZip, String sourceDir) throws IOException {
    // create a new File object based on the directory we have to zip
    File zipDir = new File(dir2zip);
    // get a listing of the directory content
    String[] dirList = zipDir.list();

    // if directory contains mimetype file, then start with it
    File mimetype_file = new File(dir2zip, MIMETYPE_FILE);
    if (mimetype_file.exists()) {
        // Set the compression ratio
        zipFile(mimetype_file, outZip, sourceDir, false);
    }

    // loop through dirList, and zip the files
    for (int i = 0; i < dirList.length; i++) {

        File f = new File(zipDir, dirList[i]);
        if (f.isDirectory()) {
            // if the File object is a directory, call this
            // function again to add its content recursively
            String filePath = f.getPath();
            zipDir(filePath, outZip, sourceDir);
            // loop again
            continue;
        }
        // Do not zip mimetype file anymore
        if (dirList[i].equals(MIMETYPE_FILE)) {
            continue;
        }

        // if we reached herem the f is not directory
        zipFile(f, outZip, sourceDir, true);
    }
}

From source file:Main.java

public static void copyFolder(File src, File dest) throws IOException {

    if (src.isDirectory()) {

        //if directory not exists, create it
        if (!dest.exists()) {
            dest.mkdir();//from w  w  w  .ja  va2 s  .c  om
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
            //construct the src and dest file structure
            File srcFile = new File(src, file);
            File destFile = new File(dest, file);
            //recursive copy
            copyFolder(srcFile, destFile);
        }

    } else {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        int length;
        //copy the file content in bytes
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();
    }
}

From source file:Main.java

public static File getFileForCrypt(String hash, Context context) {
    File externalCacheDir = new File(
            new File(new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data"),
                    context.getPackageName()),
            "cypher");
    externalCacheDir.mkdirs();//from   w w w . j ava  2  s. c  om

    // clear old items
    String[] files = externalCacheDir.list();
    long th = new Date().getTime() - 24 * 60 * 60 * 1000; // expire one day
    for (int i = 0; i < files.length; i++) {
        File tmp = new File(externalCacheDir, files[i]);
        if (tmp.lastModified() < th) {
            tmp.delete();
        }
    }

    // add new file
    File dst = new File(externalCacheDir, hash + ".tmp");
    if (dst.exists()) {
        dst.delete();
    }
    //      dst.deleteOnExit();
    return dst;
}