Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: List all directories under a given directory

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default List all directories under a given directory

    import java.util.HashMap;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
     
    //save this code to listreps.java
    //compile with >javac -Xlint listreps.java
    //example run with >java listreps c:/windows
    //the above command will list recursively all directories under the windows directory
    //I tested it on my windows directory and it listed more than 30000 sub directories.
     //So please wait that the program finishes.
     
    class rept{
    private long cnt=0;
    private String dirIn="";
    private HashMap hm = new HashMap(0,90000);
     
    void setDir(String dr){
    	dirIn=dr.replace("\\","/");
     
    }
     
     
    HashMap getReps(){
            long bk=0;
            cnt=0;
            String newline = System.getProperty("line.separator");
     
    try{
    	FileWriter facd = new FileWriter("c:/outdirs/da/da.txt",true);//path of file where directories that could not be opened are listed
    	while(true){ //4
    	   	try{
    			String listf[] = new File(dirIn).list();
    			int nlistf = listf.length;
    		     if(nlistf>0){ //3
    			       FileWriter filrep = new FileWriter("c:/outdirs/testdirs.txt",true);//path of file where directories are listed
    			       int flg=0;
    			         for(int i=0;i<nlistf;i++){ //2
    				        String pth = dirIn+"/"+listf[i];
    			   	       if(new File(pth).isDirectory()){ //1
    					         cnt=cnt+1;
    				           filrep.write(newline+cnt+","+pth);
     
    					         hm.put(cnt,pth);
    					         flg=1;
    				          }//end 1
     
    			         }//end 2
    				  filrep.close();
     
    		    }//end 3
     
    		   }catch(NullPointerException ne){
    	        	facd.write(newline+"bk="+bk+" - dirIn="+ dirIn);
    		   }catch(IOException ie){
    		   }//end catchs(ne,ie)
     
    		bk=bk+1;
    		if(bk==cnt+1) break;
     
    		dirIn=(String) hm.get(bk);		
    		}//end 4
     
    		facd.close();
     
       }catch(IOException ioe){
    	    hm.put(0,"file da not closed!");
       }//end catchs(ioe)
     
    return hm;
    }//end getReps
     
     
    long getCntreps(){
    	return cnt;
    }//end getCntrep
     
    }//end class rept
     
     
    public class listreps{
    public static void main(String args[]){
    	rept rp=new rept();
    	rp.setDir(args[0]);
    	HashMap ham=rp.getReps();
     
    	long cntreps=rp.getCntreps();
     
    System.out.println("Total number of directories="+cntreps);
    ham.clear();	
    }//end main
    }//end class listreps
    Last edited by paulngom; May 7th, 2014 at 03:55 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: List all directories under a given directory

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    paulngom (May 7th, 2014)

Similar Threads

  1. [SOLVED] Re: Java program which can list all files in a given directory
    By Dinesh Raja in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 5th, 2013, 09:27 AM
  2. Re: Java program which can list all files in a given directory
    By Paras in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2013, 07:40 AM
  3. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  4. to get name of all files from a particular directory to a list..
    By Himali in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: April 7th, 2012, 06:08 AM
  5. Java program which can list all files in a given directory
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: November 3rd, 2009, 09:42 AM