Example usage for java.io File listFiles

List of usage examples for java.io File listFiles

Introduction

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

Prototype

public File[] listFiles() 

Source Link

Document

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Usage

From source file:org.camunda.bpm.cockpit.test.util.DeploymentHelper.java

private static void addFiles(JavaArchive archive, String prefix, String rootPath, File dir) {

    for (File f : dir.listFiles()) {
        if (f.isFile()) {
            String filePath = f.getPath().replace(rootPath, "").replace("\\", "/");

            archive.addAsResource(f, prefix + filePath);
        } else {/*from   ww w. j  a  v  a2  s .com*/
            addFiles(archive, prefix, rootPath, f);
        }
    }
}

From source file:Main.java

/**
 0: number of files//from w w w .  j  a v a 2s .  com
 1: total length
 2: number of directories
 */
public static void getDirSize(final File f, final long[] l) {
    if (f.isFile()) {
        l[0]++;
        l[1] += f.length();
    } else {
        Stack<File> stk = new Stack<>();
        stk.add(f);
        File fi = null;
        File[] fs;
        while (stk.size() > 0) {
            fi = stk.pop();
            fs = fi.listFiles();
            for (File f2 : fs) {
                if (f2.isDirectory()) {
                    stk.push(f2);
                    l[2]++;
                } else {
                    l[0]++;
                    l[1] += f2.length();
                }
            }
        }
    }
}

From source file:Main.java

public static boolean removeDir(File dir) {
    boolean flag = false;
    if (dir.exists() && dir.isDirectory()) {
        File[] childFiles = dir.listFiles();
        if (childFiles == null || childFiles.length == 0) {
            flag = dir.delete();//w  ww  . j  av a 2s .  c o  m
        } else {
            for (File child : childFiles) {
                boolean del = false;
                if (child.isDirectory()) {
                    del = removeDir(child);
                } else if (child.isFile()) {
                    del = child.delete();
                }
                if (!del) {
                    break;
                }
            }
            flag = dir.delete();
        }
    }
    return flag;
}

From source file:FileUtil.java

static void showDir(int indent, File file) throws IOException {
    for (int i = 0; i < indent; i++)
        System.out.print('-');
    System.out.println(file.getName());
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++)
            showDir(indent + 4, files[i]);
    }//from www . j  a  va  2  s. c  o  m
}

From source file:Main.java

/**
 * delete file/*from  ww w.j a v  a  2 s . c om*/
 * 
 * @param file
 *            file
 * @return true if delete success
 */
public static boolean deleteFile(File file) {
    if (!file.exists()) {
        return true;
    }
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File f : files) {
            deleteFile(f);
        }
    }
    return file.delete();
}

From source file:Main.java

public static void deleteFile(final File dir) {
    if (!dir.exists()) {
        return;//  ww  w  . jav a  2  s  .c o  m
    }
    if (dir.isDirectory()) {
        final File[] filies = dir.listFiles();
        for (int i = 0; i < filies.length; i++) {
            final File curFile = filies[i];
            if (curFile.isDirectory()) {
                deleteFile(curFile);
            } else {
                curFile.delete();
            }
        }
        dir.delete();
    }
}

From source file:com.edduarte.argus.util.Constants.java

public static long folderSize(File directory) {
    long length = 0;
    File[] subFiles = directory.listFiles();
    if (subFiles != null) {
        for (File f : subFiles) {
            length += f.isFile() ? f.length() : folderSize(f);
        }//from   w  w w . j  a va  2 s.c  o  m
    }
    return length;
}

From source file:com.googlecode.jsonschema2pojo.cli.Jsonschema2Pojo.java

private static void removeOldOutput(File targetDirectory) {
    if (targetDirectory.exists()) {
        for (File f : targetDirectory.listFiles()) {
            delete(f);/*from w  ww  . j av a 2 s . com*/
        }
    }
}

From source file:Main.java

public static void getAllFileFromPath(File file, List<File> fileList, String ed) {
    if (file != null) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File tmp : files) {
                    getAllFileFromPath(tmp, fileList, ed);
                }//from ww  w  .  jav  a  2  s .  c o m
            }
        } else if (file.getName().toLowerCase().endsWith(ed)) {
            fileList.add(file);
        }
    }
}

From source file:Main.java

public static boolean DeleteFile(File file) {
    if (file.isFile()) {

        return file.delete();
    }//from   w w  w.  jav  a2s.c  o  m
    if (file.isDirectory()) {
        File[] childFile = file.listFiles();
        if (childFile == null || childFile.length == 0) {

            return file.delete();
        }
        for (File f : childFile) {
            DeleteFile(f);
        }
        return file.delete();
    }
    return false;
}