Java Path File Name nio getFileNames(List fileNames, Path dir, String sFilter)

Here you can find the source of getFileNames(List fileNames, Path dir, String sFilter)

Description

get File Names

License

Apache License

Declaration

public static void getFileNames(List<String> fileNames, Path dir, String sFilter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010, 2012 Institute for Dutch Lexicology
 *
 * Licensed 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.//from   w w  w.  j  a  v  a 2  s.  c o m
 *******************************************************************************/

import java.io.IOException;

import java.nio.file.DirectoryStream;

import java.nio.file.Files;

import java.nio.file.Path;

import java.util.List;

public class Main {
    public static void getFileNames(List<String> fileNames, Path dir, String sFilter) {
        // Validate: we can only look inside directories
        if (!dir.toFile().isDirectory()) {
            // add the file to the list
            fileNames.add(dir.toString());
            return;
        }
        // Get all the items inside "dir" that fit "sFilter"
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, sFilter)) {
            // Walk all these items
            for (Path path : stream) {
                // Add the directory to the list to be returned
                fileNames.add(path.toAbsolutePath().toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Get a list of *all* items inside [dir]
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            // Walk all the items
            for (Path path : stream) {
                // Is this item a directory?
                if (path.toFile().isDirectory()) {
                    // Look for items inside this directory
                    getFileNames(fileNames, path, sFilter);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. getFileNameFromPath(String filePath)
  2. getFileNameFromPath(String str)
  3. getFilenameOrLastPath(String path)
  4. getFileNames(List fileNames, Path dir)
  5. getFileNames(List fileNames, Path dir, String ext, boolean recursive)
  6. getFileNameWithoutExt(final Path file)
  7. getFilePath(String dirName)
  8. getFilePathName(Path filePath)
  9. getFilePathsInFolder(String sensorName)