How to find circular dependency of a string using Java
<p>If I have 2 files say ABCD.txt and DEF.txt. I need to check if the String "ABCD" is present in DEF.txt and also the string "DEF" present in ABCD.txt and write the combination to a file.</p>

<p>Totally I have around 15000 files and each file contain nearly 50 - 3000 lines has to be searched. I wrote a piece of code, its working.. but it takes one hour to display the entire list...</p>

<p>Is any better way of performing this? Please suggest me.</p>

<pre><code>   public void findCyclicDependency(){

    Hashtable&lt;String, String&gt; htFileNameList_1  = new Hashtable&lt;String, String&gt;();  
    Hashtable&lt;String, String&gt; htCyclicNameList  = new Hashtable&lt;String, String&gt;(); 

    FileWriter fwCyclicDepen = null;
    PrintWriter outFile = null;

    FileInputStream fstream = null;     
    FileInputStream fstream_1 = null; 

    DataInputStream in = null;
    BufferedReader br = null;

    DataInputStream in_1 = null;
    BufferedReader br_1 = null;

    String strSV_File_CK="";  

    boolean bFound = false;
    File fileToSearch = null;

    String strSVFileNameForComparison = "";
    String strSVDependencyFileLine = "";
    String strSVDFileLineExisting = "";
    String strCyclicDependencyOut = "";

    try {            
        File baseInputDirectory = new File(strInputPath);            

        List&lt;File&gt; baseInputDirListing = FileListing.getFileListing(baseInputDirectory);

        // Printing out the filenames for the SodaSystem
        for (File swPackage : baseInputDirListing) 
        {

            if (swPackage.isDirectory() &amp;&amp; swPackage.getName().endsWith("Plus")) {
                List&lt;File&gt; currSwPackageFileListing = FileListing.getFileListing(swPackage);
                System.out.println("\n swPackage File --&gt; " + swPackage.getName() );
                strCyclicDependencyOut = strOutputPath + "_"+ swPackage.getName() + "_CyclicDependency.xml";
        System.out.println("\n strCyclicDependencyOut File --&gt; " + strCyclicDependencyOut );
        fwCyclicDepen = new FileWriter(strCyclicDependencyOut);
        outFile = new PrintWriter(new BufferedWriter(fwCyclicDepen));
        outFile.write("&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;");
        outFile.write("&lt;CyclicDependencyFile&gt;");

                 for (File DependentFile : currSwPackageFileListing) {                        
                    strSV_File_CK = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();

                    htFileNameList_1.put(strSV_File_CK.toUpperCase(),strSV_File_CK.toUpperCase());
                 }


                for (File DependentFile : currSwPackageFileListing) 
                {                        
                    fstream = new FileInputStream(DependentFile);
                    // Get the object of DataInputStream
                    in = new DataInputStream(fstream);
                    br = new BufferedReader(new InputStreamReader(in));

                    strSVFileNameForComparison = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();

                    //Read File Line By Line
                    while ((strSVDependencyFileLine = br.readLine()) != null) 
                    {
                        bFound = false;                                            

                        if (strSVDependencyFileLine.toUpperCase().indexOf("INDICES") == -1) 
                        {
                            //Check the current line matches any of the file name in software package folder
                            if (htFileNameList_1.contains(strSVDependencyFileLine.trim().toUpperCase())
                             &amp;&amp; strSVDependencyFileLine.compareTo(strSVFileNameForComparison) != 0)
                            {  
                              bFound = true;

                              // Get the file to search
                              for (File searchFile : currSwPackageFileListing) 
                              {

                                  if((searchFile.getName().substring(0, (searchFile.getName().length() - 4)).trim()).equals(strSVDependencyFileLine))
                                  {
                                      fileToSearch = searchFile;
                                      break;
                                  }
                              }

                              // Read the file where the file name is found
                              fstream_1 = new FileInputStream(fileToSearch);

                              in_1 = new DataInputStream(fstream_1);
                              br_1 = new BufferedReader(new InputStreamReader(in_1));

                              while ((strSVDFileLineExisting = br_1.readLine()) != null)
                              {
                                  if (strSVDFileLineExisting.toUpperCase().indexOf("EXTRA") == -1) 
                                    {
                                        if (htFileNameList_1.contains(strSVDFileLineExisting.trim().toUpperCase()) &amp;&amp; bFound 
                                                &amp;&amp; strSVDFileLineExisting.compareTo(strSVDependencyFileLine) != 0 
                                                &amp;&amp; strSVDFileLineExisting.compareTo(strSVFileNameForComparison) == 0 )
                                        {

                                            if(!htCyclicNameList.containsKey(strSVDependencyFileLine) &amp;&amp; 
                                                    !htCyclicNameList.containsValue(strSVDFileLineExisting))
                                              {
                                                htCyclicNameList.put(strSVDFileLineExisting,strSVDependencyFileLine);

                                                outFile.write("&lt;CyclicDepedency FileName = \"" +  strSVDFileLineExisting + "\""+ " CyclicFileName = \"" + 
                                                    strSVDependencyFileLine + "\" /&gt;");
                                                    break;
                                              }

                                        }
                                    }

                              }

                            }         

                        }
                        else
                        {
                            bFound = false;
                        }                               

                        }//if current line &lt;&gt; 

                    }// reach each line in the current file

                outFile.write("&lt;/CyclicDependencyFile&gt;");
            } 
            outFile.flush();
            outFile.close();

        }         

    }
    catch(Exception e){
        e.printStackTrace();
    }
}
</code></pre>

<p>Thanks
Ramm</p>
