Results 1 to 2 of 2
  1. #1
    karma is offline Member
    Join Date
    May 2007
    Posts
    6
    Rep Power
    0

    Default 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?

  2. #2
    FaRuK is offline Member
    Join Date
    May 2007
    Posts
    7
    Rep Power
    0

    Default

    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"


    Java 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;
      }
    }

Similar Threads

  1. How to Search a List in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:38 PM
  2. Binary Search in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:43 PM
  3. Java Search Form
    By djcottrell in forum New To Java
    Replies: 1
    Last Post: 12-28-2007, 08:47 PM
  4. New Search engine for Java Programmers
    By coolgeek in forum Java Software
    Replies: 0
    Last Post: 07-02-2007, 08:41 PM
  5. Replies: 1
    Last Post: 06-06-2007, 05:20 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
  •