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 6 of 6

Thread: Time taken for execution is more

  1. #1
    Member
    Join Date
    Sep 2010
    Posts
    32
    My Mood
    Confused
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Exclamation Time taken for execution is more

    Hi,
    I have written the following code to extract the names of directories/files & their extensions to text files.I am using it to find out the number of files/directories in my eclipse workspace which contains 4000+ total files. The time taken for execution is more than 10 Mins !!

    Though I get the desired results , the time taken to produce it is very high !! How can I minimize it ?? Please help...

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashSet;
     
    public class DirectoryContents 
    { 
     
      static int Cnt = 0;
      ArrayList<String> file=new ArrayList<String>();
      ArrayList<String> directory=new ArrayList<String>();
      ArrayList<String> fileextn=new ArrayList<String>();
      ArrayList<String> SortedList;
     
      /*FileWriter to write FileNames into the file*/
      FileWriter fstreamfilename;
      BufferedWriter outfilename;
     
      /*FileWriter to write FileNames into the file*/
      FileWriter fstreamdirname;
      BufferedWriter outdirname;
     
      /*FileWriter to write File Extensions into the file*/
      FileWriter fstreamext;
      BufferedWriter outext;
     
      /*Counting the number of files and directories*/
      int FileCount=0;
      int DirCount=0;
     
      String SrcFileName=null;
      String FileName=null;
      String FileExt=null;
      int SrcFileLength=0;
      int ext=0;
     
     public void Process(File SrcFile) 
     { 
     
     SrcFileName=SrcFile.getName();
     SrcFileLength=SrcFile.getName().length();
     
     /*Checking whether the SrcFile is a File or Directory & whether the file contains "." at the last*/
     
     if(SrcFile.isFile() && SrcFileName.contains("."))
     {
    	 ext=SrcFileName.lastIndexOf(".");
    	 FileName=SrcFileName.substring(0,ext);
    	 FileExt=SrcFileName.substring(ext+1,SrcFileLength);
     
    	/*Adding the extracted File Extensions to ArrayList*/
     
    	if(FileExt!=null)
    	{
    	 fileextn.add(FileExt+"\n");
    	}
     
    	System.out.println("FileName:::" + FileName);
    	System.out.println("FileExtension :::"+FileExt);
     }
     
     try
     {
       if(Cnt == 0) 
        {
     
    	   /*Creating Text File to write the File Names*/ 
    	   fstreamfilename  = new FileWriter("C:/eclipseworkspace_SHK_IB/SHK/FileNames.txt");
    	   outfilename = new BufferedWriter(fstreamfilename);
     
    	   /*Creating Text File to write the Directory Names*/ 
    	   fstreamdirname  = new FileWriter("C:/eclipseworkspace_SHK_IB/SHK/DirectoryNames.txt");
    	   outdirname = new BufferedWriter(fstreamdirname);
     
    	   /*Creating Text File to write File Extensions*/
    	   fstreamext  = new FileWriter("C:/eclipseworkspace_SHK_IB/SHK/FileExtensions.txt");
    	   outext = new BufferedWriter(fstreamext);
     
     
    	   Cnt++;
    	}
     
    	try 
    	{
    	/*Checking Whether the Source file is File or Directory*/
     
    	if(SrcFile.isFile())
    	{
    		System.out.println("[FILE] " + SrcFile.getName());
    		file.add(SrcFile.getName()+"\n");
    		FileCount++;
    	}
     
    	else if (SrcFile.isDirectory()) 
    	{ 
    		System.out.println("[DIR] " + SrcFile.getName());
    	  	directory.add(SrcFile.getName()+"\n");
    	   	DirCount++;
     
    	   	/*Getting the Contets of the Sub-Directory*/
     
    	   	File[] listOfFiles = SrcFile.listFiles(); 
     
    		if(listOfFiles!=null) 
    		{ 
    		 for (int i = 0; i < listOfFiles.length; i++)
    		 {
    			 /*Checking the Sub-Directory for Directories & Files */
    			  Process(listOfFiles[i]);
    		 }
    		}
    		else 
    		{ 
    		  System.out.println("File not found"); 
    		} 
     
    	}
     
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    		System.out.println("Error");
    	}
     
    	finally
    	{
    		Object[] filenames = file.toArray();
    		Object[] directorynames = directory.toArray();
    		Object[] fileextnObj=fileextn.toArray();
     
    		int i=0;
    		for(i=0; i < filenames.length ; i++)        
    		{
    			/*Writing the Name of Files into Text File*/
    			outfilename.write((String)filenames[i]);
     
    		}
        	for(i=0; i < directorynames.length ; i++)        
        	{
    		   /*Writing the Name of Directories into Text File*/
    		   outdirname.write((String)directorynames[i]);
    		}
     
    		if(fileextnObj!=null )
    		{
    		 for(i=0;i<fileextnObj.length;i++)
    		 {
    			 outext.write((String)fileextnObj[i]);
    		 }
    		}
     
    		System.out.println("Total Number of Files :::::::::" +FileCount);
    		System.out.println("Total Number of Directories:::::::"+DirCount);
     
    	}
     
     
       	}
    	catch(Exception e)
    	{
    	  e.printStackTrace();
    	 }
     
    	 /*Creating a file with Unique File Extensions*/
     
    	 RemoveDuplicateFileExtensions("C:/eclipseworkspace_SHK_IB/SHK/FileExtensions.txt");
     }
     
     
    public void RemoveDuplicateFileExtensions(String filename) 
    {
     try
     {
       BufferedReader reader = new BufferedReader(new FileReader(filename)); 
       BufferedWriter writer = new BufferedWriter(new FileWriter("C:/eclipseworkspace_SHK_IB/SHK/FileExtensionsFilter.txt"));
     
       HashSet<String> lines = new HashSet<String>(); 
       ArrayList<String> Sortedlist;
       Object[] SortedExtnList;
       Object[] FilteredExtnList;
     
       String line;
       int i,j;
     
     
      	while ((line = reader.readLine()) != null) 
        { 
           lines.add(line); 
        } 
     
       reader.close(); 
     
       /*Copying elements from HashList to ArrayList*/
     
       Sortedlist = new ArrayList<String>(lines);
       SortedExtnList=Sortedlist.toArray();
     
       /*Removing Duplicate FileExtensions (With respect to CASE)*/
     
       for(i=0;i<SortedExtnList.length;i++)
       {
    	for(j=i+1;j<SortedExtnList.length;j++)
    	 {
    	  if(((String)SortedExtnList[i]).equalsIgnoreCase((String)SortedExtnList[j]))
    	   {
    		Sortedlist.remove(SortedExtnList[i]);
    		}
     
    	 }
       }
     
    	FilteredExtnList=Sortedlist.toArray();
     
    	/*Writing the Filtered Unique FileExtensions to text file*/
     
    	for(i=0;i<FilteredExtnList.length;i++)
    	{
    	 writer.write((String)FilteredExtnList[i]);
    	 writer.newLine(); 
    	}
     
    	writer.close(); 
     
     }
     catch(Exception e)
     {
      e.printStackTrace();
      System.out.println("Error");
      }
     finally
     {
      new File(filename).delete();
     }
    } 
     
      public static void main(String[] args) 
      { 
        String DirName = "C:/eclipseworkspace_SHK_IB/SHK"; 
        File SrcFile = new File(DirName); 
        DirectoryContents content=new DirectoryContents();
        content.Process(SrcFile); 
      } 
     
    }

    I would get the names of Files , Directories and FileExtensions in Seperate text files after the program is executed !

    Thanks !
    Last edited by rameshiit19; December 6th, 2011 at 11:40 PM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: Time taken for execution is more

    use [ code=java]your code here [/code]

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,819
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Time taken for execution is more

    Your code will not compile for me because of the jxl packages.

  4. #4
    Member
    Join Date
    Sep 2010
    Posts
    32
    My Mood
    Confused
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Time taken for execution is more

    Quote Originally Posted by Norm View Post
    Your code will not compile for me because of the jxl packages.
    You can remove jxl package and the associated lines because I have added those for future use to write the filetypes to excel file !

    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;

    WritableWorkbook FileListing=Workbook.createWorkbook(new File("C:/eclipseworkspace_SHK_IB/SHK/FileList"),ws);

    WritableSheet ListSheet=FileListing.createSheet("Sheet1",0);



    Thanks !

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Time taken for execution is more

    Quote Originally Posted by rameshiit19 View Post
    You can remove jxl package and the associated lines because I have added those for future use to write the filetypes to excel file !
    It would be just as easy, if not easier, for you to remove those lines from your original post. That way we know we're all working from the same code, and it only has to be done once (versus 10 different people having to repeat the work). Make it easier for people to help you, and you'll get better help.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    Norm (December 6th, 2011), rameshiit19 (December 6th, 2011)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,819
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Time taken for execution is more

    Try running the code with a profiler.
    The java command has an option: -Xprof that will generate a report that will show you where the code is spending time.

Similar Threads

  1. Multi-CPU Thread Execution
    By bgroenks96 in forum Threads
    Replies: 4
    Last Post: June 27th, 2011, 12:31 AM
  2. java threading execution time question
    By centenial in forum Threads
    Replies: 4
    Last Post: September 9th, 2010, 12:32 AM
  3. Question on dependency execution
    By back2grave in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 2nd, 2010, 01:50 AM
  4. Java GUi program Execution
    By Rajan in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 09:48 PM
  5. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM