Solved

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

Posted on 2004-08-24
12
227 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
  • 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 100 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

864 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now