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

public static long getFolderSize(File file) {
    long size = 0;
    File[] fileList = file.listFiles();
    for (int i = 0; i < fileList.length; i++) {
        if (fileList[i].isDirectory()) {
            size = size + getFolderSize(fileList[i]);
        } else {// ww w  . ja  va  2s.c  o  m
            size = size + fileList[i].length();
        }
    }
    return size;
}

From source file:Main.java

private static long getFolderSize(File file) {
    long size = 0;
    File[] fileList = file.listFiles();
    for (File aFileList : fileList) {
        if (aFileList.isDirectory()) {
            size = size + getFolderSize(aFileList);
        } else {/*  www.j a va 2  s .  co  m*/
            size = size + aFileList.length();
        }
    }
    return size;
}

From source file:Main.java

private static long getFileSize(File file) {
    long size = 0;
    for (File subFile : file.listFiles()) {
        if (subFile.isDirectory()) {
            size += getFileSize(subFile);
        } else {// ww w.j  a v  a  2 s.c o m
            size += subFile.length();
        }
    }
    return size;
}

From source file:Main.java

public static long getFileSize(File file) {
    long size = 0;
    for (File subFile : file.listFiles()) {
        if (subFile.isDirectory()) {
            size += getFileSize(subFile);
        } else {/*from  ww w  . j  av  a  2 s. c o m*/
            size += subFile.length();
        }
    }
    return size;
}

From source file:Main.java

private static void clearDir(String projectDir) {
    File file = new File(projectDir);
    File[] files = file.listFiles();
    if (files == null || files.length == 0) {
        return;//from w  w  w . j  a va2s .  co  m
    }
    for (File f : files) {
        if (f.isDirectory()) {
            clearDir(f.getAbsolutePath());
        } else {
            f.delete();
        }
    }
}

From source file:Main.java

/**
 * Deletes the contents of {@code dir}. Throws an IOException if any file
 * could not be deleted, or if {@code dir} is not a readable directory.
 *///from w ww .j  a  v a2 s.c om
public static void deleteContents(File dir) throws IOException {
    File[] files = dir.listFiles();
    if (files == null) {
        throw new IOException("not a readable directory: " + dir);
    }
    for (File file : files) {
        if (file.isDirectory()) {
            deleteContents(file);
        }
        if (!file.delete()) {
            throw new IOException("failed to delete file: " + file);
        }
    }
}

From source file:Main.java

private static void searchDirSort(File dirFile) {
    File[] files = dirFile.listFiles();
    List<File> dirList = new ArrayList<File>();
    if (files != null) {
        for (File file : files) {
            if (file.isDirectory()) {
                dirList.add(file);//from  ww  w . j a  va2  s  .c o m
            } else {
                if (file.getName().toLowerCase().contains(fileType.toLowerCase())) {
                    listFile.add(file);
                }
            }
        }
        for (File dir : dirList) {
            if (dir.getName().subSequence(0, 1).equals(".") || dir.getName().equals("Android")) {

            } else {
                searchDirSort(dir);
            }
        }
    }
}

From source file:Main.java

public static File[] getFolderFiles(String aPath) {
    try {//from ww  w . j  a  v a  2s. co m
        File thisFile = new File(aPath);

        return thisFile.listFiles();

    } catch (Exception exp) {
        System.out.println("Error listing file names: " + exp);
    }
    return null;
}

From source file:Main.java

public static void rm(File f) {
    if (f.isDirectory()) {
        for (File ff : f.listFiles()) {
            rm(ff);/*from w  w  w  .  ja v  a 2  s .co m*/
        }
        f.delete();
    } else if (f.isFile()) {
        f.delete();
    }
}

From source file:Main.java

public static void ls(String path) {
    int seq = 0;/*from w  w  w .  j  a  v a 2  s .c  o  m*/
    File[] child = null;
    try {
        File dir = new File(path);
        child = dir.listFiles();
    } catch (Exception e) {
        child = null;
    }

    System.out.println("Listing of: " + path);
    if (child == null) {
        // AqLog.getInstance().info("No files to delete.  srcDir: " + srcDir + ", pattern: " + pattern );
        //System.out.println("No files to delete.  srcDir: " + srcDir + ", pattern: " + pattern );
    } else {
        for (int i = 0; i < child.length; i++) {
            System.out.println(child[i].getName());
        }
    }
    System.out.println("end of listing: " + path);
}