#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    NJ, USA
    Posts
    91
    Rep Power
    13

    Search Directory or Folder for files?


    Maybe I'm just missing it, or maybe it can't be done. Is there a way to search through a folder for a file and display it if it exists?

    I'm putting together a departmental intranet site and would like to display .pdf files.

    Can I search a folder directly for a file or do I need a database?

    Thanks,
    Walt
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    A directory is considered a File in java, so look into the 'FileReader' class. Though I have not tried it, you should be able to open a directory and read what files are available I would think.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Posts
    5
    Rep Power
    0
    There are several classes in java.io for searching and manipulating directories. Here's the simplest approach.

    import java.io.File;

    class ListFiles {
    File directoryOfPdfs = new File("/mypdfs"); // Directory is just a list of files

    if(directoryOfPdfs.isDirectory() { // check to make sure it is a directory
    String filenames[] = directoryOfPdfs.list(); make array of filenames.
    }
    }

    That gives you an array of all the filenames in a given directory. Loop through and process however you want. There is also a FilenameFilter interface if you want to create filters that can be handed to the list() method.

IMN logo majestic logo threadwatch logo seochat tools logo