?
Solved

to read all the files in a directory and then its subdir. till end

Posted on 2004-08-24
12
Medium Priority
?
234 Views
Last Modified: 2010-03-31
Hi
  I have to build a utility which can verify my complete build for all files and directories
  I am making one text file in which i am storing the directories with there respective files.
but how can i handle all the files in a directory and then further it may have directory and so on. I am not getting what collection i should use to read all so that it will perform well.
I know it is recursive concept but how can be managed.
thanx
Regards
0
Comment
Question by:econcepts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11878791
public static void search(File dir)
{
   File[] files = dir.listFiles();
   for (int i=0; i<files.length; i++)
   {
      if (files[i].isDirectory())
      {
         search(files[i]);
      }
      else
      {
         // ...
      }
   }
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11878793
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 400 total points
ID: 11878804
// Process all files and directories under dir
    public static void visitAllDirsAndFiles(File dir) {
   
   
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                visitAllDirsAndFiles(new File(dir, children[i]));
            }
        }
         else
          {
                    //this is a file
          }
    }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 11879181
See this - http://www.javaalmanac.com/egs/java.io/TraverseTree.html

The code that Sudhakar posted might be similar to that one..
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11880004
If you would use a decent IDE (e.g. ECLIPSE) you would NEVER have to worry about this.
;JOOP!
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11880160
>> If you would use a decent IDE (e.g. ECLIPSE) you would NEVER have to worry about this.
???
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 11882842
ECLIPSE compiles automatically all relevant sources and even accounts for editing of sources outside
the IDE. If need be, you can even give the command "rebuild all that you know of".
;JOOP!
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11884041
Sorry sciuriware,
I didn't read the first part of the question thorougly. I  was doubting that how your answer is relevant to this question.

As you said A good IDE is always the right solution :-)


 
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11884096
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11885525
Since you are verifying files, it's better to use Breadth-First search approach. This will save time from deep mining if surface level is not completed - meaning faster responses.

code could be something like this:

verifyFiles(){
  File[] root = new File[1]; // One element array holding the root
  Vector fileList = new Vector();
  fileList.addElement(root);
  while(!fileList.isEmpty()){
     File[] files = fileList.firstElement();
     fileList.removeElement(0);
     for(i<files.length){
         if(exists(files[i])){ // Verification here, using a method called exists(files[i])
            if(files[i].isDirectory())
                fileList.addElement(files[i].listFiles());
         }
     }
  }
}


This is just the outline.

Bye.
0
 
LVL 5

Expert Comment

by:avya2k
ID: 11889135
Can this be helpful

     public static String Search(String toSrch, String inPath){
          String ret="";
          if(inPath.charAt(inPath.length()-1)!='\\' || inPath.charAt(inPath.length()-1)!='/')     inPath=inPath+"/";
          try{
               File f=new File(inPath);              
               File[] fl=f.listFiles();    
               //System.out.println(f.getAbsolutePath());          
               for(int i=0;i<fl.length;i++){    
                    //System.out.println(fl[i].getName());                                  
                    if(fl[i].isDirectory()){
                         if(fl[i].getName().toLowerCase().indexOf(toSrch.toLowerCase())!=-1)
                              ret=ret+fl[i].getAbsolutePath()+";";
                         ret=ret+Search(toSrch, fl[i].getAbsolutePath());
                    }
                    else{
                         if(fl[i].getName().toLowerCase().indexOf(toSrch.toLowerCase())!=-1)
                              ret=ret+fl[i].getAbsolutePath()+";";
                    }
               }
          }catch(Exception e){}
          return ret;
     }
     public static void main(String[] args) {
          String flist="";
          if(args.length==0)
               System.out.println("Usage: \nSearchFile <Filename to Search> [Path to Search in]");
          else{
               String pth=System.getProperty("user.dir");
               if(args.length>1)     pth=args[1];                              
               flist=Search(args[0],pth);
               System.out.println("Searching...");                        
          }              
          if(flist.length()>0){
               while(flist.indexOf(";")!=-1)
                    flist=flist.replace(';','\n');
               System.out.println("\n\n\nFiles Searched:\n"+flist);
          }          
     }
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question