how can I search folder using Java.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: how can I search folder using Java.

  1. #1
    Join Date
    Apr 2006
    Posts
    64

    Question how can I search folder using Java.

    hey,
    I want to make a programme which connects to a certain
    folder and then search all files in it. Can you tell me how it
    can be done.
    Cheers,

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    well, do you want to search within the filecontent or just within the filenames?
    the startingpoint in both cases is java.io.File and the method listFiles().
    The starting directory is defined by File rootFolder = new File("."); which selects the current directory.
    Invoking rootFolder.listFiles() will return an array of File objects which contain the files and directories in the rootFolder. Also have a view at the corresponding javadocs.

  3. #3
    Join Date
    Apr 2006
    Posts
    64
    Thanks a lot graviton,
    I just need to check the file exntentions. Futheremore, I have to read the properties of file and save them in database alongwith filename.
    Any possible solution if you can suggest.
    cheers,

  4. #4
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    I could solve that problem, but then I would spoil your party. So I will just show you, how to extract the extension from a filename:

    Code:
    // returns extension without dot, or empty string when none found
    public static final String getFileExtension(String filename){
      int indexDot = filename.lastIndexOf('.');
      if (indexDot<0) return ""; // the filename contains no dot, so has no extension
      if (indexDot+1>=filename.length()) return ""; // filename has dot, 
      // but no further extension, eg "text."
      return filename.substring(indexDot+1, filename.length());
    }
    the filename you can get with the method getName() of an File object.

  5. #5
    Join Date
    Apr 2006
    Posts
    64
    thanks buddy,
    but if could also tell me about how to extract the propeties of file. Such as size, date etc.

  6. #6
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    have a look at the javadoc for java.io.File.
    you will recognize methods such as isFile() and isDirectory() to distinguish between both,
    lastModified() to get the time the file was modified and
    length() to get the size in bytes.

  7. #7
    Join Date
    Apr 2006
    Posts
    64
    Thanks dude,
    Really helpful. I hope I can also find methods to find other properties of file.
    cheers mate, kool

  8. #8
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    hi,
    what more properties do you need?
    i'm not shure, if java supports more. (actualy, the file implementation of java is quiet skinny).

  9. #9
    Join Date
    Apr 2006
    Posts
    64
    well if the file is Audio or video I have to get the resulotion, duration, Track info etc. etc.

  10. #10
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    62
    The JMF may contain methods for retreiving this kind of information. I have also done it through the Java Sound API; Look at the AudioFileFormat class for more information

    Kind regards,
    Noel
    Efficiency is intelligent laziness

  11. #11
    Join Date
    Apr 2006
    Posts
    64
    Thank you very much for reply noelob. Hope it will work.
    Have a nice week end.
    Best Regards
    nadz

  12. #12
    Join Date
    Mar 2005
    Location
    UK, London
    Posts
    150
    below is a link to a program what will list files in a directory, it will give you the file size of each file. hope it helps.

    http://www.java.codeyourself.com/Download.php?ID=28

  13. #13
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    whow, that's quiet tricky. the file class just encapsulates information about a file, not it's content.
    however there are several apis and classes that can handle multimedia filetypes in java. so you could use such a class to open files with the corresponding extension and check their properties.
    for audio there is the package javax.sound.sampled and it su**s. so the api of choice for audio information should be athttp://www.jsresources.org/.
    They have an example of getting audiofile information at http://www.jsresources.org/examples/AudioFileInfo.html.
    They also support MIDI files and some MP3 encodings.
    For Video files, there is the jmf (java media framework) in which i never got involved. i'll have a look at it at a later time, next week.
    but that's what you will have to do: look for apis, that support retrieving information about the different media types you want to support.
    perhaps there is an api out there, like irfanview in java, which is able to handle all of the media types you need.

  14. #14
    Join Date
    Apr 2006
    Posts
    64
    hmm seems more difficult then I thought
    Thanks
    and have a nice weekend

Similar Threads

  1. Breadth first search with java
    By Ant_Magma in forum Java
    Replies: 26
    Last Post: 03-07-2006, 09:12 AM
  2. Replies: 0
    Last Post: 02-01-2002, 12:08 AM
  3. JAVADOTNET !!!
    By Dharmesh in forum .NET
    Replies: 4
    Last Post: 10-01-2001, 04:47 PM
  4. Re: ALERT - XP COULD DISABLE JAVA IN WEB BROWSERS AND EMAI L
    By Tim Romano in forum Talk to the Editors
    Replies: 1
    Last Post: 07-27-2001, 09:05 AM
  5. Java Developers and Java Architect for exciting projects in Chicago area
    By Keith Franklin, MCSD in forum java.announcements
    Replies: 0
    Last Post: 08-18-2000, 07:37 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center