Results 1 to 4 of 4
  1. #1
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Print File Extensions Only

    Hey crew- I am needing to perform a recursive file search print the file extensions ONLY of all files

    I am using this to print full file names but how could this be tweaked (or another line of code all together) to only give me the extensions,
    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
             try (Stream<Path> paths = Files.walk(Paths.get("/home/ja/spg"))) {
                paths.filter(Files::isRegularFile)
                        .forEach(System.out::println);
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    But how could I have the code give me an output of the extensions, so I would get something like:
    .txt
    .csv
    .xlsx
    .html

    etc?
    Last edited by jo15765; 03-03-2018 at 07:02 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    16

    Default Re: Print File Extensions Only

    According to the Iterable documentation, forEach() takes a Consumer argument. System.out::println is a method reference that can act as a Consumer. But I doubt that PrintStream offers a method that does just what you want.

    The Consumer documentation linked to above strongly hints that a lambda expression might be useful.

    The lambda expression will have to be designed so that you give an unambiguous meaning to "extension" in all cases. Eg what is the extension of "System32" or "backup.tar.gz"? (Sorry if that's obvious.)
    Last edited by pbrockway2; 03-03-2018 at 09:35 PM. Reason: sorry. early. not enough coffee

  3. #3
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    63
    Rep Power
    0

    Default Re: Print File Extensions Only

    Quote Originally Posted by pbrockway2 View Post
    According to the Iterable documentation, forEach() takes a Consumer argument. System.out::println is a method reference that can act as a Consumer. But I doubt that PrintStream offers a method that does just what you want.

    The Consumer documentation linked to above strongly hints that a lambda expression might be useful.

    The lambda expression will have to be designed so that you give an ambiguous meaning to "extension". Eg what is the extension of "System32" or "backup.tar.gz"? (Sorry if that's obvious.)
    I think the answer to your question would be for my purpose to take the last "." in the filename string and print such that. so for example "backup.tar.gz" would print .gz as it is the last "." in the string

    Does that help clarify?

    Also, I am not sure if this will affect an answer, but I am building this on a Windows machine, but would like the GUI to be portable to a Unix/Linux machine as well :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    16

    Default Re: Print File Extensions Only

    You can define "extension" to mean whatever you want. Taking everything from the last "dot" is fine - although you still have to worry about cases where there is no dot, or where the filename ends in a dot, etc. The concept of extension is sort of defined in Windows but not defined at all on *nix machines. I'm not saying it isn't useful (it is!), but it's up to you to decide on a precise meaning and implement it.

    More importantly, did you understand the stuff linked to describing Iterable, Consumer, and lambda expressions? I daresay a google search will turn up workable code, but the links were intended to try and provide some context which will be helpful in lots of cases.

    The bottom line here is: replace "System.out::println" with a lambda expression that gets the toString() form of the path, extracts the portion you regard as the extension, and prints it.

Similar Threads

  1. Scan Directory For File Extensions
    By chalupabatman in forum New To Java
    Replies: 26
    Last Post: 07-22-2016, 04:28 PM
  2. Lucene File Extensions supported and unsupported
    By shanti.dhanus in forum Lucene
    Replies: 0
    Last Post: 11-06-2012, 10:34 AM
  3. checking file extensions
    By gisler in forum New To Java
    Replies: 3
    Last Post: 12-05-2009, 01:25 PM
  4. Declaring Custom File Extensions
    By Jesdisciple in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-07-2008, 10:44 PM
  5. How to compare file extensions?
    By Rubin in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 09:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •