Java File Name Get getFileNames_STR(String path)

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

Description

get File NameSTR

License

Open Source License

Declaration

public static String[] getFileNames_STR(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 {
    public static String[] getFileNames_STR(String path) {

        List<File> al = getFiles(path);
        String[] fl = null;//  w  w  w  . j a  va2s.com
        if (al != null && !al.isEmpty()) {
            fl = new String[al.size()];
            for (int i = 0; i < al.size(); i++) {
                fl[i] = al.get(i).getName();
            }
        }

        return fl;
    }

    /**
     * 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. getFilenames(String dirctory)
  2. getFileNames(String directory, String file)
  3. getFileNames(String dirPath)
  4. getFilenames(String filenames)
  5. getFileNames(String path, boolean withFullPath)
  6. getFileNamesFromDir(File rootDir, String indexName)
  7. getFileNamesInJar(File source, String folder, String extension)
  8. getFileNamesOld(File zipFile)
  9. getFileNamesRecursive(final String pDataDir, final FilenameFilter pFilter, final int pMaxFiles)