Here you can find the source of getSortedPathList(final Path dir, final boolean dirsFirst)
Parameter | Description |
---|---|
dir | a parameter |
dirsFirst | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static List<Path> getSortedPathList(final Path dir, final boolean dirsFirst) throws IOException
//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; } }