Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-25-2007, 08:32 PM
Member
 
Join Date: May 2007
Posts: 6
karma is on a distinguished road
How to search files with Java
I want to search for a specific file in the computer and I want know if the file exists and if so I want to know the path of it. Does anybody know how can i do this?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-26-2007, 02:25 PM
Member
 
Join Date: May 2007
Posts: 7
FaRuK is on a distinguished road
The following is a sample, and may have rooms for improvement.
To perform this, after compiling, enter a command as a command line input

java FindFilterdFile [directory from which serch begins] [filter]

For example like this.

java FindFilterdFile "C:\\" ".jpg"


Code:
import java.util.*; import java.io.*; final public class FindFilteredFile { final static void perform(File Dir, MyFilter filtr) throws IOException{ if (Dir==null) return; File[] filtered = Dir.listFiles(filtr); if (filtered==null) return; Arrays.sort(filtered); for(int i = 0; i < filtered.length; i++){//print Filtered Filename if(filtered[i].isFile()) { System.out.println(filtered[i].getCanonicalPath()); } } //end of for } final public static void main(String[] args) { if(args.length !=2){ System.out.println("Enter path and filter."); return; } File iDir = new File(args[0].trim()); //e.g. "." MyFilter filt = new MyFilter(args[1].trim()); //e.g.".jpg" if(!(iDir.exists())||(iDir.isFile())){ System.out.println("Directory does not exist."); return; } FindDir finder = new FindDir(); ArrayList objs = null; try{ objs = finder.getDs(iDir); }catch(Exception e){e.printStackTrace();} finder = null; try{ Thread.sleep(400);// affords to run back ground System.gc(); }catch(InterruptedException trx){;} try{ Collections.sort(objs); Iterator it = objs.iterator(); while(it.hasNext()){ FindFilteredFile.perform((File)(it.next()), filt); } }catch(IOException iioo){iioo.printStackTrace();} return; } //end of main } //end of FindFilteredFile final class FindDir{ private ArrayList objD; FindDir(){ this.objD = new ArrayList(); } public ArrayList getDs(File givendir){ this.enumDs(givendir); return this.objD; } private final void enumDs(File dir){ (this.objD).add(dir); File[] dirArr = dir.listFiles(); if (dirArr==null) return; for(int i=0;i < dirArr.length;i++){ if(dirArr[i].isDirectory()){ this.enumDs(dirArr[i]); }else{continue;} //end of if } //end of for dirArr = null; } } //end of FindDir final class MyFilter implements FilenameFilter { String extension; String f; MyFilter(String ext) { this.extension = ext; } public boolean accept(File directory, String f_name) { f = new File(f_name).getName(); boolean result = (f.indexOf(extension) !=-1); f = null; return result; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Search a List in Java Java Tip java.lang 0 04-16-2008 11:38 PM
Binary Search in Java Java Tip Algorithms 0 04-15-2008 08:43 PM
Java Search Form djcottrell New To Java 1 12-28-2007 08:47 PM
New Search engine for Java Programmers coolgeek Java Announcements 0 07-02-2007 08:41 PM
File Upload - Single to allow the search of .txt files Daniel Advanced Java 1 06-06-2007 05:20 AM


All times are GMT +3. The time now is 11:17 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org