Java File Find findByExt(File base, String ext)

Here you can find the source of findByExt(File base, String ext)

Description

find By Ext

License

Apache License

Declaration

public static File[] findByExt(File base, String ext) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.util.Vector;

public class Main {

    public static File[] findByExt(File base, String ext) {
        // searches recursively in base directory
        // for files that match the extension ext

        Vector result = new Vector();

        findByExtWorker(result, base, ext);

        // make generic array into a type-safe array
        Object objs[] = result.toArray();
        File files[] = new File[objs.length];
        System.arraycopy(objs, 0, files, 0, objs.length);

        return files;
    }/*from  w  w w . j av  a2s. c om*/

    private static void findByExtWorker(Vector result, File base, String ext) {
        File files[] = base.listFiles();
        if (files == null) {
            return;
        }

        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (!file.isDirectory()) {
                if (ext.equals("*")) {
                    result.add(file);
                } else {
                    String currentExt = getFileExtension(file);
                    if (currentExt.equalsIgnoreCase(ext)) { // bingo; add to
                        // result set
                        result.add(file);
                    }

                }

            } else { // file is a directory, recurse
                findByExtWorker(result, file, ext);
            }
        }
    }

    public static boolean isDirectory(String fileName) {
        File testFile = new File(fileName);

        if ((testFile.exists()) && (testFile.isDirectory())) {
            return true;
        } else {
            return false;
        }
    }

    public static String getFileExtension(String filename) {
        // returns extension including .
        // Now strip of possible extension.

        String extension = "";
        int index = filename.lastIndexOf('.');
        if (index != -1)
            extension = filename.substring(index);

        return extension;
    }

    public static String getFileExtension(File file) {
        // returns extension including .

        // Strip of path first.
        String base = file.getName();
        // Now strip of possible extension.

        String extension = "";
        int index = base.lastIndexOf('.');
        if (index != -1)
            extension = base.substring(index);

        return extension;
    }

    public static String getFileExtension(String filename, boolean keepDot) {
        filename = filename.replace('\\', '/');

        String name;
        int namePos = filename.lastIndexOf('/');
        if (namePos != -1) {
            name = filename.substring(namePos + 1);
        } else {
            // no path info found
            name = filename;
        }

        String ext;
        int extPos = name.lastIndexOf('.');
        if (extPos != -1) {
            if (keepDot)
                ext = name.substring(extPos);
            else
                ext = name.substring(extPos + 1);
        } else {
            // no extension found
            ext = "";
        }

        return ext;
    }

    public static String getFileExtension(File file, boolean keepDot) {
        // Strip path first.
        String base = file.getName();

        String extension = "";
        int index = base.lastIndexOf('.');
        if (index != -1) {
            if (keepDot)
                extension = base.substring(index);
            else
                extension = base.substring(index + 1);
        }
        return extension;
    }
}

Related

  1. find(String executable, File... dirs)
  2. find(String name, File dir)
  3. find(String path, String matcher)
  4. find(String path, String suffix)
  5. find2(File baseDir, FileFilter filter, List files, boolean includeHiddenFiles)
  6. findByExtension(final File directory, final String extension)
  7. findByFileName(List files, String fileName)
  8. findExe(String exeName, String... path)
  9. findExecutable(File baseLocation)