Solved

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

Posted on 2004-08-24
12
229 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 104
Java - Why doesn't this JFrame work 3 59
ForLoop Example 3 48
null output 3 25
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

776 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