{"id":2941,"date":"2013-03-19T18:44:10","date_gmt":"2013-03-19T18:44:10","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2941"},"modified":"2013-03-19T18:44:10","modified_gmt":"2013-03-19T18:44:10","slug":"java-how-to-get-all-the-files-inside-a-folder-and-its-subfolders-without-recursion","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/java-how-to-get-all-the-files-inside-a-folder-and-its-subfolders-without-recursion\/","title":{"rendered":"java: How to get all the files inside a folder and its subfolders without recursion"},"content":{"rendered":"<p>Most programmers will do this in a recursive fashion, but doing that is putting yourself at risk of hitting a stack overflow error, and it&#8217;s 20% slower (according to my tests). Here&#8217;s my first implementation of a method that will return just the files (cause I didn&#8217;t need the folders, you can always hack it to your needs). This is part of FrostWire for Android and FrostWire for Desktop, all the code is available under the GPL at our <a href=\"https:\/\/github.com\/frostwire\/frostwire-android\" target=\"_blank\">github repository<\/a>.<\/p>\n<p>[java]<br \/>\n\/** Given a folder path it&#8217;ll return all the files contained within it and it&#8217;s subfolders<br \/>\n     * as a flat set of Files.<br \/>\n     *<br \/>\n     * Non-recursive implementation, up to 20% faster in tests than recursive implementation. \ud83d\ude42<br \/>\n     *<br \/>\n     * @author gubatron<br \/>\n     * @param folder<br \/>\n     * @param extensions If you only need certain files filtered by their extensions, use this string array (without the &#8220;.&#8221;). or set to null if you want all files. e.g. [&#8220;txt&#8221;,&#8221;jpg&#8221;] if you only want text files and jpegs.<br \/>\n     *<br \/>\n     * @return The set of files.<br \/>\n     *\/<br \/>\n    public static Collection<File> getAllFolderFiles(File folder, String[] extensions) {<br \/>\n        Set<File> results = new HashSet<File>();<br \/>\n        Stack<File> subFolders = new Stack<File>();<br \/>\n        File currentFolder = folder;<br \/>\n        while (currentFolder != null &#038;&#038; currentFolder.isDirectory() &#038;&#038; currentFolder.canRead()) {<br \/>\n            File[] fs = null;<br \/>\n            try {<br \/>\n                fs = currentFolder.listFiles();<br \/>\n            } catch (SecurityException e) {<br \/>\n            }<\/p>\n<p>            if (fs != null &#038;&#038; fs.length > 0) {<br \/>\n                for (File f : fs) {<br \/>\n                    if (!f.isDirectory()) {<br \/>\n                        if (extensions == null || FilenameUtils.isExtension(f.getName(), extensions)) {<br \/>\n                            results.add(f);<br \/>\n                        }<br \/>\n                    } else {<br \/>\n                        subFolders.push(f);<br \/>\n                    }<br \/>\n                }<br \/>\n            }<\/p>\n<p>            if (!subFolders.isEmpty()) {<br \/>\n                currentFolder = subFolders.pop();<br \/>\n            } else {<br \/>\n                currentFolder = null;<br \/>\n            }<br \/>\n        }<br \/>\n        return results;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>Here&#8217;s the <a href=\"https:\/\/github.com\/frostwire\/frostwire-android\/blob\/master\/src\/common\/com\/frostwire\/android\/util\/FilenameUtils.java\" target=\"_blank\">FilenameUtils<\/a> if you want the isExtension() implementation and it&#8217;s dependencies, you can always code your own there<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most programmers will do this in a recursive fashion, but doing that is putting yourself at risk of hitting a stack overflow error, and it&#8217;s 20% slower (according to my tests). Here&#8217;s my first implementation of a method that will return just the files (cause I didn&#8217;t need the folders, you can always hack it [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[15],"tags":[1437,1227,583,1228],"class_list":["post-2941","post","type-post","status-publish","format-standard","hentry","category-code","tag-code","tag-examples","tag-java","tag-utilities"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-Lr","jetpack-related-posts":[{"id":2762,"url":"https:\/\/www.gubatron.com\/blog\/frostwire-for-android-breaks-past-its-first-250k-lines-of-source-code\/","url_meta":{"origin":2941,"position":0},"title":"FrostWire for Android breaks past its first 250k lines of source code","author":"gubatron","date":"July 31, 2012","format":false,"excerpt":"-------------------------------------------------------------------------------- Language files blank comment code -------------------------------------------------------------------------------- Java 1950 101133 77855 256590 XML 126 480 2351 9308 XSD 2 28 57 187 Bourne Again Shell 1 30 17 147 -------------------------------------------------------------------------------- SUM: 2079 101671 80280 266232 -------------------------------------------------------------------------------- Help wanted","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":945,"url":"https:\/\/www.gubatron.com\/blog\/new-frostwire-4171-released\/","url_meta":{"origin":2941,"position":1},"title":"New FrostWire 4.17.1 Released","author":"gubatron","date":"November 8, 2008","format":false,"excerpt":"Source FOR IMMEDIATE RELEASE: Official FrostWire 4.17.1 Download link FrostWire.com -- The last of the 4.17.x FrostWire series is finally out. The following is a list of the most important updates made for this release, which irons out most of the complains received from the community in regards to 4.17.0.\u2026","rel":"","context":"In &quot;FrostWire&quot;","block_context":{"text":"FrostWire","link":"https:\/\/www.gubatron.com\/blog\/category\/frostwire\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2759,"url":"https:\/\/www.gubatron.com\/blog\/frostwire-nearing-1mm-lines-of-source-code\/","url_meta":{"origin":2941,"position":2},"title":"FrostWire nearing 1MM lines of Source Code","author":"gubatron","date":"July 31, 2012","format":false,"excerpt":"-------------------------------------------------------------------------------- Language files blank comment code -------------------------------------------------------------------------------- Java 6700 230082 339218 818509 HTML 403 8751 4745 72384 XML 70 1112 996 11029 C++ 6 220 317 806 CSS 10 202 132 799 JSP 16 64 606 777 Javascript 5 77 54 571 XSLT 4 134 298 553 XSD 7 40\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3020,"url":"https:\/\/www.gubatron.com\/blog\/how-to-avoid-android-compilationbuilding-issues-setting-up-the-eclipse-buildpath-correctly\/","url_meta":{"origin":2941,"position":3},"title":"How to avoid Android compilation\/building issues &#8211; Setting up the Eclipse Buildpath correctly","author":"gubatron","date":"May 21, 2013","format":false,"excerpt":"This post is the product of about 48 hours of not being able to work on what I was supposed to be working because I had to deal with build issues in Eclipse. Hopefully If you follow these steps you will be able to solve some of the issues you\u2026","rel":"","context":"In &quot;Android&quot;","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/category\/android\/"},"img":{"alt_text":"Screen Shot 2013-05-21 at 12.13.46 PM","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2013\/05\/Screen-Shot-2013-05-21-at-12.13.46-PM.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3150,"url":"https:\/\/www.gubatron.com\/blog\/how-to-add-an-existing-git-repository-to-github\/","url_meta":{"origin":2941,"position":4},"title":"How to add an existing GIT repository to github.","author":"gubatron","date":"August 31, 2013","format":false,"excerpt":"Most of the times, it makes more sense to start working on something that slowly transforms into the beginning of a project that deserves to be on github. This post is about creating a local repository and putting it on github. 1. First we must convert the main local folder\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3064,"url":"https:\/\/www.gubatron.com\/blog\/new-frostwire-for-android-1-0-8-out\/","url_meta":{"origin":2941,"position":5},"title":"New FrostWire for Android 1.0.8 out","author":"gubatron","date":"June 1, 2013","format":false,"excerpt":"A new FrostWire for Android is out on Google Play. In this release we improved our search algorithm to include relevant search results that were being hidden in previous versions. We've also made use of Square's Picasso Image loading\/caching library for displaying all the images on the user interface, resulting\u2026","rel":"","context":"In \"Android\"","block_context":{"text":"Android","link":"https:\/\/www.gubatron.com\/blog\/tag\/android\/"},"img":{"alt_text":"Screenshot of the new FrostWire for Android navigation menu","src":"https:\/\/frostwire.files.wordpress.com\/2013\/05\/468570_620137218016084_1578847253_o.png?w=350&h=200&crop=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2941"}],"version-history":[{"count":1,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2941\/revisions"}],"predecessor-version":[{"id":2942,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2941\/revisions\/2942"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}