Java File List Load getFileList(File[] fileArray)

Here you can find the source of getFileList(File[] fileArray)

Description

Get a list of files from a file array, where the directory entries are recursively resolved by all profile files inside the directory.

License

Open Source License

Parameter

Parameter Description
fileArray array of files

Return

list of files

Declaration

public static List<File> getFileList(File[] fileArray) 

Method Source Code

//package com.java2s;
/* $Id$/*  w w w  .j  av  a2 s .  co m*/
 *****************************************************************************
 * Copyright (c) 2009 Contributors - see below
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    thn
 *****************************************************************************
 *
 * Some portions of this file was previously release using the BSD License:
 */

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Main {
    /**
     * Get a list of files from a file array, where the directory entries
     * are recursively resolved by all profile files inside the directory.
     * 
     * @param fileArray array of files
     * @return list of files
     */
    public static List<File> getFileList(File[] fileArray) {
        List<File> files = new ArrayList<File>();
        for (int i = 0; i < fileArray.length; i++) {
            File file = fileArray[i];
            files.addAll(getList(file));
        }
        return files;
    }

    private static List<File> getList(File file) {
        List<File> results = new ArrayList<File>();
        List<File> toDoDirectories = new LinkedList<File>();
        Set<File> seenDirectories = new HashSet<File>();
        toDoDirectories.add(file);
        while (!toDoDirectories.isEmpty()) {
            File curDir = toDoDirectories.remove(0);
            if (!curDir.isDirectory()) {
                // For some reason, this alleged directory is a single file
                // This could be that there is some confusion or just
                // the normal, that a single file was selected and is
                // supposed to be imported.
                results.add(curDir);
                continue;
            }
            // Get the contents of the directory
            File[] files = curDir.listFiles();
            if (files != null) {
                for (File curFile : curDir.listFiles()) {
                    // The following test can cause trouble with
                    // links, because links are accepted as
                    // directories, even if they link files. Links
                    // could also result in infinite loops. For this
                    // reason we don't do this traversing recursively.
                    if (curFile.isDirectory()) {
                        // If this file is a directory
                        if (!seenDirectories.contains(curFile)) {
                            toDoDirectories.add(curFile);
                            seenDirectories.add(curFile);
                        }
                    } else {
                        String s = curFile.getName().toLowerCase();
                        if (s.endsWith(".xmi")
                                || s.endsWith(".uml") // for AndroMDA profiles
                                || s.endsWith(".xmi.zip")
                                || s.endsWith(".xml.zip")) {
                            results.add(curFile);
                        }
                    }
                }
            }
        }
        return results;
    }
}

Related

  1. getFileList(File file)
  2. getFileList(File file)
  3. getFileList(File file, String[] suffixs)
  4. getFileList(File folder, String type)
  5. getFileList(File sourceDirectory, final String extension)
  6. getFileList(final File dir, final String extension, final List list, final int maxDepth)
  7. getFileList(final List fileList, final File root, final File[] ignoreList)
  8. getFileList(final String location)
  9. getFileList(Map>> map, File dir, T key)