import java.io.*; 
import java.util.*; 
import java.io.FileWriter; 
import java.io.IOException; 


public class Dirtrav
{
public static void main(String args[])
{
      
   // String rootDir="C:\\Projects\\PerfBVT'\\W6_TopicVsQueue_8.2.0.0.74_040110\\";
      File directory = new File("C:\\Projects\\PerfBVT'\\W6_TopicVsQueue_8.2.0.0.74_040110");
      identifydir(directory); 

}  
	
	//File directory = new File("C:\\Projects\\PerfBVT'\\W6_TopicVsQueue_8.2.0.0.74_040110\\");  
     

   public static void identifydir(File di)
   {
	         File[] files = di.listFiles();
	  
	         if (! di.exists()) 
             {
	           System.out.println("Invalid path");
	           return;
	          }
	           if (di.isDirectory()) 
               {
	
	                  
       	              for (int i = 0; i < files.length; i++)  
                   {  
                        //Print out the name of files in the directory  
                       //System.out.println(files[index].toString());  
           	             System.out.println(files[i]);
		               if (files[i].isFile())
		                 {
				           //System.out.println(di);
                             System.out.println("filefound");				 
				            
						    
						 }
		                    di=files[i];
						  System.out.println("Directory name :"+di.toString());
						  Generate.generateCsvFile("C:\\resultsexp\\Topic.csv",di.toString());
						  
						  
						    //
						  
		                     identifydir(di);
		           }  
	
	             }  
    }  
}

this code traverse through the directory and sub directory...i need to pass the directory as an input for another program...however here it shows file also as a directory....that leads to an file not found error in the other code ....can anyone please suggest a solution for this problem...thanks in advance

Recommended Answers

All 6 Replies

you can create an Arraylist, and whenver isDirectory is true. add it in ur arraylist.. pass that list as a parameter in other call.

As the first line of your "identify" method add if (!di.isDirectory()) return;

if (!di.isDirectory()) return; not working.......please help..

define "not working".

import java.io.*; 
import java.util.*; 
import java.io.FileWriter; 
import java.io.IOException; 


public class Dirtrav
{
public static void main(String args[])
{
      
      File directory = new File("C:\\Projects\\Broker\\Linux_P-Idcfiqa2_S-232");
      identifydir(directory); 

}  
	 

   public static void identifydir(File di)
   {
	         File[] files = di.listFiles();
	  
	         if (!di.isDirectory()) 
			{
				return;
			}
			
			 if (! di.exists()) 
            {
	           System.out.println("Invalid path");
	           return;
	        } 
	        
			if (di.isDirectory()) 
            {   
			   
	            for (int i = 0; i < files.length; i++)  
                {  
                        //Print out the name of files in the directory  
                       //System.out.println(files[index].toString());  
           	    
		              					   
					   if (files[i].isFile())
		                {
				       	}
						else 
						{
						System.out.println(files[i]);
						}
						
		                di=files[i];
						Generate.generateCsvFile("C:\\resultsexp\\Topic.csv",di.toString());
					    identifydir(di);
		            }   
	
	              
            }         
        }
    }

i did this......now i'm able to send only directory....i just wanted to check before the recurssive call..if it was a directory or not ...if (!di.isDirectory()) return; was not able to filter and send only directory.....
thanks for ur quick reply..

Like I said. Make it the first line of the method. It does no good to check whether or not it is a directory after you have already called listFiles on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.