Results 1 to 3 of 3
  1. #1
    jhoang is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default iterate through filenames in directory

    I am trying to iterate through a directory and a search for any file that contains .log in its name, and save these names into a string array. The directory which I want to search is where ever the .class or .jar will be located (i.e. if i put the program onto the desktop, it will look through all file names existing on the desktop). Is there a way to do this?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,530
    Rep Power
    5

    Default Re: iterate through filenames in directory

    Check out the File class and FileFilter interface in the JDK API.

    Regards,
    Jim
    Last edited by jim829; 06-10-2013 at 10:25 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: iterate through filenames in directory

    Quote Originally Posted by jhoang View Post
    I am trying to iterate through a directory and a search for any file that contains .log in its name, and save these names into a string array.
    Check out the API for File#list(...) / listFiles(...) that take a parameter of type FilenameFilter.

    You might also find javax.swing.filechooser.FileNameExtensionFilter useful.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How do I iterate
    By Nilla in forum New To Java
    Replies: 7
    Last Post: 06-23-2012, 08:23 AM
  2. Continuing Filenames
    By Stamoulohta in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 06:37 PM
  3. Display filenames in frame
    By cool in forum AWT / Swing
    Replies: 1
    Last Post: 11-19-2010, 02:51 PM
  4. How to iterate through two Arrays?
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 05-18-2010, 07:48 AM
  5. Replies: 5
    Last Post: 01-27-2010, 09:14 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
  •