Java Path File List nio getSortedPathList(final Path dir, final boolean dirsFirst)

Here you can find the source of getSortedPathList(final Path dir, final boolean dirsFirst)

Description

Supposed to be faster than getting the list and sorting it.

License

Open Source License

Parameter

Parameter Description
dir a parameter
dirsFirst a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static List<Path> getSortedPathList(final Path dir, final boolean dirsFirst) throws IOException 

Method Source Code

//package com.java2s;
/*//w  w w.j  av  a  2s  .  c  o  m
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * 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
 */

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

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main {
    /**
     * Supposed to be faster than getting the list and sorting it. Sort as we go.
     * @param dir
     * @param dirsFirst
     * @return
     * @throws IOException
     */
    public static List<Path> getSortedPathList(final Path dir, final boolean dirsFirst) throws IOException {

        if (!Files.isDirectory(dir))
            return null;

        final Map<String, Path> files = new TreeMap<String, Path>();
        final Map<String, Path> dirs = new TreeMap<String, Path>();

        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir)) {
            for (Path file : directoryStream) {

                if (dirsFirst && Files.isDirectory(file)) {
                    dirs.put(file.getFileName().toString(), file);
                } else {
                    files.put(file.getFileName().toString(), file);
                }
            }
        }

        final List<Path> ret = new ArrayList<Path>(files.size() + dirs.size());
        ret.addAll(dirs.values());
        ret.addAll(files.values());
        files.clear();
        dirs.clear();
        return ret;
    }
}

Related

  1. fileListDeep(Path dir)
  2. findExpectedResultForTestcase(final Path testcase, final LinkedList expectedResults)
  3. getClassLoaderFromPaths(ArrayList paths)
  4. getDirectoryList(Path basePath)
  5. getPreorderList(Path root)
  6. getTables(final List d1)
  7. isBlackListedDirectory(Path path)
  8. isPlaylistFile(Path testFile)
  9. list(final Path directory)