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 folderSize(File directory) {
    long length = 0;
    File[] files = directory.listFiles();
    if (files != null)
        for (File file : files)
            if (file.isFile())
                length += file.length();
            else//w  w  w . j  av a  2s . c o  m
                length += folderSize(file);
    return length;
}

From source file:Main.java

/**
 * Delete directory you passed. the directory's child file and directory will be deleted too by recursive exploring.
 *///from www .  ja v  a  2 s  . c  om
public static void deleteAllFiles(File directory) {
    File[] files;

    files = directory.listFiles();
    files = files == null ? new File[0] : files;

    for (File file : files) {
        if (file.isDirectory())
            deleteAllFiles(file);
        else
            file.delete();
    }
    directory.delete();
}

From source file:Main.java

private static long calculateDirectorySize(File directory) {
    long result = 0;
    for (File file : directory.listFiles()) {
        if (file.isDirectory()) {
            result += calculateDirectorySize(file);
        } else {//  ww  w.  j  av a2s  .com
            result += file.length();
        }
    }
    return result;
}

From source file:Main.java

private static void deleteFlie(String path) {
    if (TextUtils.isEmpty(path)) {
        return;//from ww  w . ja v a 2 s .  com
    }
    File filePath = new File(path);
    File[] itemList = filePath.listFiles();
    long totalSize = 0;
    if (null != itemList) {
        for (File f : itemList) {
            if (f.exists() && f.isFile()) {
                f.delete();
            }
        }
    }
}

From source file:Main.java

public static void delete(File file) {
    if (file.isDirectory()) {
        File[] childs = file.listFiles();
        for (File file2 : childs)
            delete(file2);/*www. j  a v  a 2 s .c  o m*/
    }
    file.delete();
}

From source file:Main.java

public static ArrayList<File> refreshFileList(String strPath) {
    ArrayList<File> filelist = new ArrayList<File>();
    File dir = new File(strPath);
    File[] files = dir.listFiles();

    if (files == null)
        return null;
    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            refreshFileList(files[i].getAbsolutePath());
        } else {//from ww w  . ja va 2 s .c  om
            if (files[i].getName().toLowerCase().endsWith("zip"))
                filelist.add(files[i]);
        }
    }
    return filelist;
}

From source file:Main.java

public static void deletePhotoAtPathAndName(String path, String fileName) {
    if (checkSDCardAvailable()) {
        File folder = new File(path);
        File[] files = folder.listFiles();
        for (int i = 0; i < files.length; i++) {
            System.out.println(files[i].getName());
            if (files[i].getName().equals(fileName)) {
                files[i].delete();/* w w  w  . j  av  a2  s  .c om*/
            }
        }
    }
}

From source file:Main.java

public static File[] getFolderFiles(URI aPathURI) {
    try {//from  w ww  .  j  a  v  a2  s. c  o  m
        File thisFile = new File(aPathURI);

        return thisFile.listFiles();

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

From source file:Main.java

private static void deleteFileAndAnyContents(File file) {
    if (file.isDirectory())
        for (File child : file.listFiles()) {
            deleteFileAndAnyContents(child);
        }// w ww.j av a2 s . c o m

    file.delete();
}

From source file:Main.java

private static long getFileSize(String path) {
    if (TextUtils.isEmpty(path)) {
        return 0;
    }// www  .  j  a  va  2 s .c  o  m
    File filePath = new File(path);
    File[] itemList = filePath.listFiles();
    long totalSize = 0;
    if (null != itemList) {
        for (File f : itemList) {
            if (f.exists() && f.isFile() && f.length() > 0) {
                totalSize = totalSize + f.length();
            }
        }
    }
    return totalSize;
}