Java Folder Read getFiles(String path)

Here you can find the source of getFiles(String path)

Description

getFiles

License

Open Source License

Parameter

Parameter Description
path a parameter

Declaration

public static List<File> getFiles(String path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    /**//  ww w  .  j  a  v  a 2 s  .  c o  m
     * getFiles
     * 
     * @param path
     * @return
     */
    public static List<File> getFiles(String path) {

        File dir = new File(path);

        if (dir.exists()) {

            File[] files = dir.listFiles();

            if (files != null && files.length > 0) {
                return initDirs(files);
            }
        }

        return Collections.emptyList();
    }

    /**
     * existFile
     * 
     * @param path
     * @return
     */
    public static boolean exists(String path) {

        try {
            File temp = new File(path);
            return temp.exists();
        } catch (Exception e) {
            // ignore
        }
        return false;
    }

    public static List<File> initDirs(File[] files) {

        ArrayList<File> dirs = new ArrayList<File>();

        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile() || files[i].isDirectory()) {
                dirs.add(files[i]);
            }
        }

        return dirs;
    }

    /**
     * isFile
     * 
     * @param path
     * @return
     */
    public static boolean isFile(String path) {

        File file = new File(path);

        if (file.exists() && file.isFile()) {
            return true;
        }

        return false;
    }
}

Related

  1. getFiles(String path)
  2. getFiles(String path)
  3. getFiles(String path)
  4. getFiles(String path)
  5. getFiles(String PATH)
  6. getFiles(String path)
  7. getFiles(String path)
  8. getFiles(String path, final String suffix)
  9. getFiles(String path, String endian)