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 !