Java Path File List nio listFiles(Path basePath)

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

Description

Returns the list of files in a folder using NIO API.

License

Open Source License

Parameter

Parameter Description
basePath The parent folder

Exception

Parameter Description
IOException an exception

Return

The list of files

Declaration

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

Method Source Code

//package com.java2s;
/*/*  w w w.j a v a 2 s .  com*/
 * Copyright (C) 2014-2015 CS-SI (foss-contact@thor.si.c-s.fr)
 * Copyright (C) 2014-2015 CS-Romania (office@c-s.ro)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see http://www.gnu.org/licenses/
 */

import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

public class Main {
    /**
     * Returns the list of files in a folder using NIO API.
     *
     * @param basePath    The parent folder
     * @return The list of files
     * @throws IOException
     */
    public static List<Path> listFiles(Path basePath) throws IOException {
        return listFiles(basePath, 1);
    }

    /**
     * Returns the list of files in a folder, up to the given depth of the folder,
     * using NIO API.
     *
     * @param basePath    The parent folder
     * @param depth     The depth to look for files
     * @return The list of files
     * @throws IOException
     */
    public static List<Path> listFiles(Path basePath, int depth) throws IOException {
        if (basePath == null)
            return null;
        depth = depth <= 0 ? 255 : depth;
        List<Path> files = new ArrayList<>();
        Files.walkFileTree(basePath, EnumSet.noneOf(FileVisitOption.class), depth, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                files.add(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
        return files;
    }
}

Related

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