Java Path File List nio listFiles(Path dir)

Here you can find the source of listFiles(Path dir)

Description

list Files

License

Apache License

Declaration

public static List<Path> listFiles(Path dir) throws IOException 

Method Source Code

//package com.java2s;
/*/*w  w  w .j  ava  2s. c om*/
 *   ________________________________________________________________________________________
 *   
 *   Y O O R E E K A
 *   A library for data mining, machine learning, soft computing, and mathematical analysis
 *   ________________________________________________________________________________________ 
 *    
 *   The Yooreeka project started with the code of the book "Algorithms of the Intelligent Web " 
 *   (Manning 2009). Although the term "Web" prevailed in the title, in essence, the algorithms 
 *   are valuable in any software application.
 *  
 *   Copyright (c) 2007-2009 Haralambos Marmanis & Dmitry Babenko
 *   Copyright (c) 2009-${year} Marmanis Group LLC and individual contributors as indicated by the @author tags.  
 * 
 *   Certain library functions depend on other Open Source software libraries, which are covered 
 *   by different license agreements. See the NOTICE file distributed with this work for additional 
 *   information regarding copyright ownership and licensing.
 * 
 *   Marmanis Group LLC licenses this file to You under the Apache License, Version 2.0 (the "License"); 
 *   you may not use this file except in compliance with the License.  
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software distributed under 
 *   the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *   either express or implied. See the License for the specific language governing permissions and
 *   limitations under the License.
 *   
 */

import java.io.IOException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<Path> listFiles(Path dir) throws IOException {
        List<Path> result = new ArrayList<>();
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            for (Path entry : stream) {
                result.add(entry);
            }
        } catch (DirectoryIteratorException ex) {
            // I/O error encountered during the iteration
            throw ex.getCause();
        }
        return result;
    }

    /**
     * List all the files with the <tt>allowedExtensions</tt> within <tt>dir</tt> 
     * 
     * @param directoryPath
     * @param allowedExtensions, e.g. <tt>{c:h:cpp:hpp:java}</tt>
     * @return
     * @throws IOException
     */
    public static List<Path> listFiles(Path directoryPath, String allowedExtensions) throws IOException {

        List<Path> result = new ArrayList<>();

        String[] ext = allowedExtensions.split(":");

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(directoryPath)) {
            for (Path entry : stream) {
                if (entry.toFile().isDirectory()) {
                    //recurse
                    result.addAll(listFiles(entry, allowedExtensions));
                } else {
                    for (String s : ext) {
                        if (entry.toString().endsWith(s)) {
                            result.add(entry);
                        }
                    }
                }
            }
        } catch (DirectoryIteratorException ex) {
            // I/O error encounted during the iteration, the cause is an IOException
            throw ex.getCause();
        }
        return result;
    }
}

Related

  1. listDir(Path dir)
  2. listDirsRecursive(String pathStr, String pattern, int maxDepth)
  3. listFiles(final Path path)
  4. listFiles(Path base, StringBuilder b)
  5. listFiles(Path basePath)
  6. listFiles(Path dir, String glob)
  7. listFiles(Path directory)
  8. listFiles(Path directory, String glob)
  9. listFiles(Path path)