Solved

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

Posted on 2004-08-24
12
226 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:mayankeagle
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:mayankeagle
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

16 Experts available now in Live!

Get 1:1 Help Now