Java Folder Read getFilesByType(String fileType, String path)

Here you can find the source of getFilesByType(String fileType, String path)

Description

get Files By Type

License

Open Source License

Declaration

public static List<File> getFilesByType(String fileType, String path) 

Method Source Code


//package com.java2s;
/*//  w w w  .ja  va 2  s .  co  m
 * @(#)FileHelper.java      Created at 2013-2-24
 * 
 * Copyright (c) 2011-2013 azolla.org All rights reserved.
 * Azolla PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
 */

import java.io.File;

import java.util.List;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

public class Main {
    /**
     * filter files by file type.
     * 
     * @param fileType file type
     * @param files   file list
     * @return  file list of the file end with file type
     */
    public static List<File> getFilesByType(String fileType, List<File> files) {
        Preconditions.checkNotNull(files);
        List<File> fileList = Lists.newArrayList();
        if (Strings.isNullOrEmpty(fileType)) {
            return fileList;
        }
        for (File f : files) {
            if (!f.isDirectory()) {
                if (fileType.equalsIgnoreCase(getFileType(f))) {
                    fileList.add(f);
                }
            }
        }
        return fileList;
    }

    /**
     * @see org.azolla.io.FileHelper#getFilesByType(String, List<File>)
     * @see org.azolla.io.FileHelper#allFiles(String)
     */
    public static List<File> getFilesByType(String fileType, String path) {
        return getFilesByType(fileType, allFiles(path));
    }

    /**
     * @see org.azolla.io.FileHelper#getFileType(String)
     */
    public static String getFileType(File file) {
        Preconditions.checkNotNull(file);
        return getFileType(file.getName());
    }

    /**
     * return type of file by file name
     * example:test.txt -> txt
     * 
     * @param fileName file name
     * @return type of file
     */
    public static String getFileType(String fileName) {
        Preconditions.checkNotNull(fileName);
        int lastPointIndex = fileName.lastIndexOf(".");
        if (-1 == lastPointIndex) {
            return fileName;
        } else {
            return fileName.substring(lastPointIndex + 1);
        }
    }

    /**
     * Return all document under this directory and sub directory.
     * if the file is document return it.
     * 
     * Return empty when:
     * 1.file not exist
     * 2.file is an empty directory
     * 
     * Throw exception with the file is null.
     * 
     * @param file document or directory
     * @return the container with document
     */
    public static List<File> allFiles(File file) {
        Preconditions.checkNotNull(file);
        List<File> allFiles = Lists.newArrayList();
        if (file.exists()) {
            if (file.isDirectory() && file.list() != null) {
                for (File f : file.listFiles()) {
                    allFiles.addAll(allFiles(f));
                }
            } else {
                allFiles.add(file);
            }
        }
        return allFiles;
    }

    /**
     * @see org.azolla.io.FileHelper#allFiles(File)
     */
    public static List<File> allFiles(String path) {
        return allFiles(new File(path));
    }
}

Related

  1. getFilesAsArrayList(String path)
  2. getFilesByDate(String directory, final boolean earliestFirst)
  3. getFilesByName(File path, String name)
  4. getFilesByPath(String toDir)
  5. getFilesByRegxFileName(String dir, final String regxName)
  6. getFilesByType(String path, final String type)
  7. getFilesCannotAccess(File... files)
  8. getFilesComposingPath(File aFile)
  9. getFilesCount(File archiveFile)