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

public static void clearCache(Context context) {

    File cache = context.getCacheDir();
    String[] files = cache.list();
    for (String file : files) {
        File data = new File(cache.getPath() + "/" + file);
        data.delete();/*from ww  w.  j ava2  s. c  om*/
    }
}

From source file:Main.java

public static List<String> getSubFolderList(String dir) {
    List<String> results = new ArrayList<String>();
    File file = new File(dir);
    String[] names = file.list();

    for (String name : names) {
        if (new File(dir + "/" + name).isDirectory()) {
            results.add(name);//from ww  w  .  ja  v  a2  s  .  c  o  m
        }
    }
    return results;
}

From source file:Main.java

public static void visitAllDirsAndFiles(File dir) {
    System.out.println(dir);//from www  .ja  v a2 s.  c om

    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            visitAllDirsAndFiles(new File(dir, children[i]));
        }
    }
}

From source file:Main.java

public static void deleteFile(File file) {
    if (file.isDirectory()) {
        if (file.list().length == 0)
            file.delete();/*  w  ww  .ja  v  a  2s  . c o m*/
        else {
            File[] files = file.listFiles();
            for (File f : files)
                deleteFile(f);
            deleteFile(file);
        }
    } else
        file.delete();
}

From source file:Main.java

public static void clearDirectory(File dir) {
    String[] list = dir.list();
    if (list != null) {
        for (int i = 0; i < list.length; i++) {
            File file = new File(dir, list[i]);
            file.delete();/*from w  w w.  j  a v  a 2  s  . c  o m*/
        }
    }
}

From source file:Main.java

public static boolean deleteAll(File dir) {
    String fileNames[] = dir.list();
    if (fileNames == null)
        return false;
    for (int i = 0; i < fileNames.length; i++) {
        File file = new File(dir, fileNames[i]);
        if (file.isFile())
            file.delete();/*from  ww w  .j  av a 2  s.c  om*/
        else if (file.isDirectory())
            deleteAll(file);
    }

    return dir.delete();
}

From source file:Main.java

public static void visitAllDirs(File dir) {
    if (dir.isDirectory()) {
        System.out.println(dir);/*from  w  ww.  j  a v  a 2 s. c  o  m*/

        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            visitAllDirs(new File(dir, children[i]));
        }
    }
}

From source file:Main.java

public static String[] getFolderFileNames(String aPath) {
    try {//from   ww w.  ja  va  2s  .co m
        File thisFile = new File(aPath);

        return thisFile.list();

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

From source file:Main.java

public static String[] getFolderFileNames(URI aPathURI) {
    try {//from  w w w. j a  va2 s.  c  om
        File thisFile = new File(aPathURI);

        return thisFile.list();

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

From source file:Main.java

public static boolean deleteFolder(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteFolder(new File(dir, children[i]));
            if (!success) {
                return false;
            }/*  w w w.  ja v  a 2 s  . c om*/
        }
    }
    return dir.delete();
}