Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-24
12
Medium Priority
?
238 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

604 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