Java Path File List nio list(Path basePath)

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

Description

list

License

Open Source License

Declaration

public static String[] list(Path basePath) throws IOException 

Method Source Code

//package com.java2s;
/*//from   ww w  .ja  v  a 2  s.co  m
 * 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 {
    public static String[] list(Path basePath) throws IOException {
        if (basePath == null)
            return null;
        List<String> files = new ArrayList<>();
        Files.walkFileTree(basePath, EnumSet.noneOf(FileVisitOption.class), 1, 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.toAbsolutePath().toString());
                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.toArray(new String[files.size()]);
    }
}

Related

  1. getSortedPathList(final Path dir, final boolean dirsFirst)
  2. getTables(final List d1)
  3. isBlackListedDirectory(Path path)
  4. isPlaylistFile(Path testFile)
  5. list(final Path directory)
  6. listChildren(Path dir)
  7. listChildren(Path dir, boolean searchDirectory)
  8. listChildren(Path directory)
  9. listChildren(Path directory)