Results 1 to 6 of 6
  1. #1
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default Java equivalent of Linux 'find' function

    Hello all. I hope you can all help me find an answer to my question.

    I am looking for a Java equivalent to the 'find' command in Linux. Basically something that when given a directory it goes and finds everything underneath that directory recursively.

    Is there any sort of pre-existing class in the JDK that can do that?

    I have been looking around at the 1.6 API. In particular in java.io which is where I thought I might find something like this but so far I have found nothing that fits my needs.

    For some additional info I need to generate a list of the complete filepaths of each file beneath a certain directory. Sometimes the files will be more than one directory deep so it needs to be recursive.

    Any suggestions would be helpful. If I had to code something like this myself I don't know where I would start. If I have to code it myself though then I will.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Read the API documentation for the File class; it has methods that can do (part of) the job.

    kind regards,

    Jos

  3. #3
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Read the API documentation for the File class; it has methods that can do (part of) the job.

    kind regards,

    Jos
    Ah so I see.

    With methods such as getAbsolutePath() and list() it could be done with loops checking each array element returned by list() recursively until it reaches a point at which there are no more directories.

    One could write each file encountered (according to the API list() would return null for files so I might not even need to use isDirectory()) to a log file and then use that file as an input later.

    OK at least I have a plan moving forward should I be unable to find a class that will do this for me.

    Another possibility is to call a linux script from inside the class that then performs the 'find'. I imagine this is also possible.

    Thank you for that tip.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by porchrat View Post
    Another possibility is to call a linux script from inside the class that then performs the 'find'. I imagine this is also possible.
    You'd lose portability, i.e. it will only run on a Linux box while a pure Java solution would run everywhere.

    kind regards,

    Jos

  5. #5
    porchrat is offline Senior Member
    Join Date
    Mar 2009
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You'd lose portability, i.e. it will only run on a Linux box while a pure Java solution would run everywhere.

    kind regards,

    Jos
    Yea that is a problem. Right now everything is pretty universal.

    Though it probably will never run on anything other than a Linux box in the near futeure that may change later and I don't want some stupid little area of the thing binding it to a single platform when I could just put in some extra effort now and get what I need.

    Actually I'm going to try and see how that find function works as well that should be able to point me in the right direction as to how best to tackle this in Java.

    It looks like this sort of functionality doesn't exist as a preformed Java class. I'm probably going to have to build it myself. However if someone does happen across this thread and recall a class that will do this or perhaps some code in the public domain that I can use as a guide please don't hesitate to let me know.

    So far the help has been very enlightening and is greatly appreciated. Thank you Jos :)

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default

    Hi,

    You might also want to look for the following interface java.io.FilenameFilter if you want to filter the file listed base on name or extension name for instance. You need to implements the accept method and do the filtering inside this method.

Similar Threads

  1. SWT program on Linux can find jar files.
    By glmarsh in forum SWT / JFace
    Replies: 1
    Last Post: 07-20-2009, 08:02 PM
  2. Cannot find Main class in Linux OS
    By msskiran in forum Eclipse
    Replies: 0
    Last Post: 04-10-2009, 03:04 AM
  3. [SOLVED] Java equivalent to X11 Work Procedure
    By LongHitter in forum New To Java
    Replies: 0
    Last Post: 01-12-2009, 12:36 PM
  4. Filter a file in java (unix cut command equivalent)
    By marcosabel in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 08:26 PM
  5. Php explode function, equivalent java
    By fernando in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 06:44 AM

Posting Permissions

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