给定路径得到本路径下文件夹以及所有文件的列表排序问题

2015年01月12日 其他资源 8条评论 阅读4585次

给定路径得到本路径下文件夹以及所有文件的列表排序问题

本文主要解决的是排序问题:也就是给定路径下要求,文件夹在前文件在后,并且按照数字字母先后顺序排列

private List<File> getFileList(String path) {

        final String ROOT = Environment.getExternalStorageDirectory()
                .toString();
        List<File> fileList = new ArrayList<File>();

        File topFile = new File(path);
        for (File file : topFile.listFiles()) {
            fileList.add(file);
        }

        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile()) {
                    // 文件夹在文件的前面
                    return -1;
                }
                if (o1.isFile() && o2.isDirectory()) {
                    // 文件在文件夹的后面
                    return 1;
                }
                // 同为文件或同为文件夹,a-z、0-9的顺序排列
                // 因为compareTo是按照ASCII码排序,所以忽略大小写,统一用小写字母
                return -(o2.getName().toLowerCase().compareTo(o1.getName()
                        .toLowerCase()));
            }
        });

        if (path != ROOT) {
            // if the current path is not ROOT_PATH, add ../ at the first
            fileList.add(0, new File(path).getParentFile());
        }

        return fileList;
    }
分享本文至:

WRITTEN BY

avatar
本文标签:排序sort
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

8 条评论

  1. avatar auroragenri

    <a href="http://www.magnindesign.com/nike-5.0-free-grau-schuhek">nike 5.0 free grau</a> <a href="http://www.spinecareuk.com/kvinners-nike-air-max-tailwind-6-all-gr%C3%B8nn-scarpeg">kvinners nike air max tailwind 6 all gr酶nn</a> <a href="http://www.danteb.com/air-jordan-1-michigan-for-salg-kurpesb">air jordan 1 michigan for salg</a> <a href="http://www.karlisteffy.com/nike-air-jordan-eclipse-negro-01-zapatosk">nike air jordan eclipse negro 01</a> <a href="http://www.festadoano.com/yeezy-750-boost-solar-rouge-instructions-runningc">yeezy 750 boost solar rouge instructions</a> <a href="http://www.bursagti.com/nike-air-max-thea-verde-and-gris-zapatosj">nike air max thea verde and gris</a>
    auroragenri http://www.auroragenri.com/

  2. avatar prformusic

    <a href="http://www.wuxiangtaichi.com/air-jordan-retro-7-femminile-gituttio-scarpei">air jordan retro 7 femminile gituttio</a> <a href="http://www.culdesacmag.com/nike-zoom-pegasus-32-lila-schwarz-schuheh">nike zoom pegasus 32 lila schwarz</a> <a href="http://www.livinggreedy.com/curry-3-red-shoesl">curry 3 red</a> <a href="http://www.clickstroy.com/boys-chicago-bulls-jersey-nfli">boys chicago bulls jersey</a> <a href="http://www.zotorrent.com/kvinners-nike-air-max-tailwind-8-r%C3%B8d-gul-obuvk">kvinners nike air max tailwind 8 r酶d gul</a> <a href="http://www.viptarimas.com/coach-trifold-womens-wallet-coachh">coach trifold womens wallet</a>
    prformusic http://www.prformusic.com/

  3. avatar karmacanix

    <a href="http://www.tmzred.com/nike-kobe-10-elite-alle-gr%C3%A5-skol">nike kobe 10 elite alle gr氓</a> <a href="http://www.alcovozov.com/femminile-nike-zoom-pegasus-31-tutti-nero-scarpeh">femminile nike zoom pegasus 31 tutti nero</a> <a href="http://www.ahmztradefx.com/nike-flyknit-lunar-3-verde-arancia-scarpei">nike flyknit lunar 3 verde arancia</a> <a href="http://www.isampled.com/air-jordan-retro-11-rosa-royal-blau-schuheh">air jordan retro 11 rosa royal blau</a> <a href="http://www.aldervets.com/air-jordan-true-flight-white-orange-shoesl">air jordan true flight white orange</a> <a href="http://www.vanltl.com/san-jose-sharks-marleau-jersey-nfli">san jose sharks marleau jersey</a>
    [url=http://www.karmacanix.com/]karmacanix[/url]

  4. avatar lockip

    <a href="http://www.sundialproductions.net/yeezy-boost-750-marron-kitchen-obuvc">yeezy boost 750 marron kitchen</a> <a href="http://www.survivewatercrisis.net/adidas-zx-700-noir-cuir-kit-scarped">adidas zx 700 noir cuir kit</a> <a href="http://www.szczybelski.net/nike-shox-deliver-marina-militare-hours-obuvc">nike shox deliver marina militare hours</a> <a href="http://www.tammyjdalton.net/adidas-pr%C3%B8dator-pro-junior-chaussuresg">adidas pr酶dator pro junior</a> <a href="http://www.tastingtheacorn.net/roshe-run-oreo-gituttio-chaussuresa">roshe run oreo gituttio</a> <a href="http://www.technologyhandyman.net/nike-huarache-svart-zalando-butya">nike huarache svart zalando</a>
    lockip http://www.lockip.net/

  5. avatar 王语双

    外行路过,留一爪印,闪人。

  6. avatar 威客

    现在做安卓的越来越多

  7. avatar Han

    安卓主流现在已成定局

欢迎留言




用户登录

sitemap