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:JarMaker.java

/**
 * Deletes all files and subdirectories under dir.
 * @param dir/*from   ww w  . ja  v  a2  s . com*/
 * @return true if all deletions were successful.If a deletion fails, the
 *         method stops attempting to delete and returns false.
 */
public 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:de.unirostock.sems.cbarchive.web.VcImporter.java

private static void scanRepositoryDir(File base, File dir, List<File> relevantFiles) {

    String[] entries = dir.list();
    // nothing to scan in this dir
    if (entries == null)
        return;//from   ww w. j a  va2 s  .  c  om

    // looping throw all directory elements
    for (int index = 0; index < entries.length; index++) {
        File entry = new File(dir, entries[index]);

        if (entry.isDirectory() && entry.exists() && !entry.getName().startsWith(".")) {
            // Entry is a directory and not hidden (begins with a dot) -> recursive
            scanRepositoryDir(base, entry, relevantFiles);
        } else if (entry.isFile() && entry.exists()) {
            // Entry is a file -> check if it is relevant

            relevantFiles.add(entry);
        }

    }

}

From source file:forge.util.FileUtil.java

public static boolean isDirectoryWithFiles(final String path) {
    final File f = new File(path);
    return f.exists() && f.isDirectory() && f.list().length > 0;
}

From source file:Main.java

/**
Remove a directory and all of its contents.
        //  w w  w . j  av a 2s. c  o  m
The results of executing File.delete() on a File object
that represents a directory seems to be platform
dependent. This method removes the directory
and all of its contents.
        
@return true if the complete directory was removed, false if it could not be.
If false is returned then some of the files in the directory may have been removed.
        
*/
public static boolean removeDirectory(File directory) {

    // System.out.println("removeDirectory " + directory);

    if (directory == null)
        return false;
    if (!directory.exists())
        return true;
    if (!directory.isDirectory())
        return false;

    String[] list = directory.list();

    // Some JVMs return null for File.list() when the
    // directory is empty.
    if (list != null) {
        for (int i = 0; i < list.length; i++) {
            File entry = new File(directory, list[i]);

            //        System.out.println("\tremoving entry " + entry);

            if (entry.isDirectory()) {
                if (!removeDirectory(entry))
                    return false;
            } else {
                if (!entry.delete())
                    return false;
            }
        }
    }

    return directory.delete();
}

From source file:com.geewhiz.pacify.utils.FileUtils.java

private static void copyDirectoryRecursively(File source, File target) throws IOException {
    if (!target.exists()) {
        Files.copy(source.toPath(), target.toPath(), java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
    }/*from  www .  j a  v  a2 s .  c  o  m*/

    for (String child : source.list()) {
        copyDirectory(new File(source, child), new File(target, child));
    }
}

From source file:forge.util.FileUtil.java

public static boolean deleteDirectory(File dir) {
    if (dir.isDirectory()) {
        for (String filename : dir.list()) {
            if (!deleteDirectory(new File(dir, filename))) {
                return false;
            }//w  w  w .  java 2 s. co m
        }
    }
    return dir.delete();
}

From source file:Main.java

static boolean copyFiles(File sourceLocation, File targetLocation) throws IOException {
    if (sourceLocation.equals(targetLocation))
        return false;

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();//  www. jav  a 2  s . c o  m
        }
        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyFiles(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else if (sourceLocation.exists()) {
        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
    return true;
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

private static void zipContents(File dir, String zipPath, ZipOutputStream zos) {
    String[] children = dir.list();
    if (children == null) {
        return;/*w  w  w.  j  a  va2 s.c o  m*/
    }
    for (String currentChild : children) {
        File child = new File(dir, currentChild);
        String childZipPath = zipPath + File.separator + child.getName();
        if (child.isDirectory()) {
            zipContents(child, childZipPath, zos);
        } else {
            try {
                zip(child, childZipPath, zos);
            } catch (Exception e) {
                throw new JoboxException(e);
            }
        }
    }
}

From source file:com.bb.extensions.plugin.unittests.internal.utilities.FileUtils.java

/**
 * Delete the directory if and only if it is empty
 * /* w w w. jav  a2s  .  co  m*/
 * @param dir
 *            The directory to delete
 * @return True if the directory was deleted, false otherwise
 */
private static boolean deleteDirectoryIfEmpty(File dir) {
    if (dir.isDirectory() && (dir.list().length > 0)) {
        return false;
    } else {
        return dir.delete();
    }
}

From source file:com.jalios.ejpt.sync.utils.IOUtil.java

public static void deepDelete(File file) {

    // Recursive calls
    if (file.isDirectory()) {
        String[] list = file.list();
        for (int i = 0; i < list.length; i++) {
            deepDelete(new File(file, list[i]));
        }/*w w w.jav a2  s . c  om*/
    }

    // Perform delete
    boolean deleted = file.delete();
    if (!deleted) {
        logger.warn("Cannot delete file " + file);
    }
}