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 void deleteFile(File file) {
    if (file.isDirectory()) {
        if (file.list().length == 0)
            file.delete();/*from ww w.j  av  a2  s . c  o  m*/
        else {
            File[] files = file.listFiles();
            for (File f : files)
                deleteFile(f);
            deleteFile(file);
        }
    } else
        file.delete();
}

From source file:Main.java

private static void readGpxDirectory(File dir, final Map<String, Long> map, String parent,
        boolean absolutePath) {
    if (dir != null && dir.canRead()) {
        File[] files = dir.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.getName().toLowerCase().endsWith(".gpx")) { //$NON-NLS-1$
                    map.put(absolutePath ? f.getAbsolutePath() : parent + f.getName(), f.lastModified());
                } else if (f.isDirectory()) {
                    readGpxDirectory(f, map, parent + f.getName() + "/", absolutePath);
                }/*w  ww.ja  v  a  2 s .c o m*/
            }
        }
    }
}

From source file:Main.java

public static List<File> listFilesByName(File f) {
    Log.d("listFileByName f", "" + f);
    final List<File> fList = new LinkedList<>();
    final Stack<File> stk = new Stack<>();
    if (f.isDirectory()) {
        stk.push(f);/*  w  w w  .jav  a  2  s. c o  m*/
    } else {
        fList.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);
            } else {
                fList.add(f2);
            }
        }
    }
    return fList;
}

From source file:hrider.io.FileHelper.java

public static void delete(File path, String... exclude) {
    List<String> excludedPaths = Arrays.asList(exclude);

    File[] files = path.listFiles();
    if (files != null) {
        for (File file : files) {
            if (!excludedPaths.contains(file.getName())) {
                if (file.isDirectory()) {
                    delete(file);//from   w ww  .j  ava2s . com
                }
                file.delete();
            }
        }
    }
}

From source file:net.landora.video.info.file.FileHasher.java

public static void checkDirectory(File dir) throws IOException {
    for (File file : dir.listFiles()) {
        if (file.isHidden()) {
            continue;
        }/*w ww  .  j a v a 2s  .  c om*/

        if (file.isDirectory()) {
            checkDirectory(file);
        } else {

            String extension = ExtensionUtils.getExtension(file);
            if (!ExtensionUtils.isVideoExtension(extension)) {
                continue;
            }

            FileInfo info = FileInfoManager.getInstance().getFileInfo(file);
            VideoMetadata md = MetadataProvidersManager.getInstance().getMetadata(info);

            if (md == null) {
                continue;
            }

            file = file.getCanonicalFile();

            String filename = getOutputFilename(md) + "." + extension;

            File outputFile = new File(getOutputFolder(md), filename);
            outputFile.getParentFile().mkdirs();
            outputFile = outputFile.getCanonicalFile();

            if (!outputFile.equals(file)) {
                if (outputFile.exists()) {
                } else {
                    FileUtils.moveFile(file, outputFile);
                }

            }

        }
    }
}

From source file:Main.java

/**
 * Delete the earliest file in the specified directory
 * //from  ww w.j ava 2  s .  c o  m
 * @param dir
 *            The specified directory
 * @param exceptFile
 *            Exclude the file name
 */
public static final void deleteEarliestFile(File dir, String exceptFile) {
    if (dir != null && dir.isDirectory()) {
        File earlyFile = null;
        File[] files = dir.listFiles();
        if (files.length == 0)
            return;
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.getName().equals(exceptFile))
                continue;
            if (earlyFile == null) {
                earlyFile = files[i];
                continue;
            }
            if (earlyFile.lastModified() > f.lastModified()) {
                earlyFile = f;
            }
        }
        if (earlyFile != null)
            earlyFile.delete();
    }
}

From source file:Main.java

static DefaultMutableTreeNode createTreeModel(File file, boolean recursive) {
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(file);
    if (file.isDirectory() && recursive) {
        File[] children = file.listFiles();
        if (children != null) {
            for (File f : children) {
                node.add(createTreeModel(f, recursive));
            }//from  ww w. j av  a2 s . c o  m
        }
    }
    return node;
}

From source file:Main.java

public static ArrayList<File> getFileList(File dir, String... fileExtensions) {
    final ArrayList<File> files = new ArrayList<File>();
    final File[] fileList = dir.listFiles();
    if (fileList != null) {
        for (File file : fileList) {
            if (file.isFile()) {
                if (fileExtensions.length == 0) {
                    files.add(file);/*from www. j  ava 2s .  c o m*/
                } else {
                    String fileName = file.getName().toLowerCase();
                    for (String ext : fileExtensions) {
                        if (fileName.endsWith(ext)) {
                            files.add(file);
                            break;
                        }
                    }
                }
            } else {
                files.addAll(getFileList(file, fileExtensions));
            }
        }
    }
    return files;
}

From source file:Main.java

public static long getDirectorySize(File f) {
    if (f == null || !f.exists() || !f.isDirectory()) {
        return 0;
    }//w w w . j  ava  2s .  com
    long size = 0;
    File[] flist = f.listFiles();
    if (flist != null) {
        for (int i = 0; i < flist.length; i++) {
            if (flist[i].isDirectory()) {
                size = size + getDirectorySize(flist[i]);
            } else {
                size = size + flist[i].length();
            }
        }
    }
    return size;
}

From source file:com.oldratlee.nanotemplate.NanoTemplateTest.java

@Parameterized.Parameters
public static Collection<Object[]> prepareData() throws Exception {
    final List<Object[]> retTestData = new ArrayList<Object[]>();

    File directory = new File(NanoTemplateTest.class.getResource("./").getFile());
    File[] files = directory.listFiles();
    assertNotNull(files);/*w  w  w .j a  v a 2 s.com*/
    for (File f : files) {
        if (f.getName().endsWith("nanotemplate")) {
            retTestData.add(new Object[] { f.getName() });
        }
    }
    return retTestData;
}