开发者

How to find circular dependency of a string using Java

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.

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...

Is any better way of performing this? Please suggest me.

   public void findCyclicDependency(){

    Hashtable<String, String> htFileNameList_1  = new Hashtable<String, String>();  
    Hashtable<String, String> htCyclicNameList  = new Hashtable<String, String>(); 

    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<File> baseInputDirListing = FileListing.getFileListing(baseInputDirectory);

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

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

                 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())
                             && 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()) && bFound 
                                                && strSVDFileLineExisting.compareTo(strSVDependencyFileLine) != 0 
                                                && strSVDFileLineExisting.compareTo(strSVFileNameForComparison) == 0 )
                                        {

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

                                                outFile.write("<CyclicDepedency FileName = \"" +  strSVDFileLineExisting + "\""+ " CyclicFileName = \"" + 
                                                    strSVDependencyFileLine + "\" />");
                                                    break;
                                              }

                                        }
                                    }

                              }

                            }         

                        }
                        else
                        {
                            bFound = false;
                        }                               

                        }//if current line <> 

                    }// reach each line in the current file

                outFile.write("</CyclicDependencyFile>");
            } 
            outFile.flush();
            outFile.close();

        }         

    }
    catch(Exception e){
        e.printStackTrace();
    }
}

Thanks Ramm


There are several problems with your design. The most important one is repeated scanning of the file system. Try the code below.

static public void findCyclicDependency2() {
    PrintWriter outFile = null;

    Map<String,File> fileNames = new HashMap<String,File>();
    Map<String,Set<String>> fileBackward = new HashMap<String,Set<String>>();
    Map<String,Set<String>> fileForward = new HashMap<String,Set<String>>();

    try {
        File baseInputDirectory = new File(strInputPath);

        List<File> baseInputDirListing = getFileListing(baseInputDirectory);

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

            if (! (swPackage.isDirectory()
                    || swPackage.getName().endsWith("Plus"))) continue;

            System.out.println("Loading file names");
            List<File> currSwPackageFileListing = getFileListing(swPackage);
            for(File dependentFile:currSwPackageFileListing) {
                String name = trimName(dependentFile);
                fileNames.put(name,dependentFile);

                BufferedReader br = new BufferedReader(new FileReader(dependentFile));
                String line;
                Set<String> contFor = new HashSet<String>();
                Set<String> contBack = new HashSet<String>();
                while( (line=br.readLine()) != null ) {
                    line = line.toUpperCase().trim();
                    if( line.equals("EXTRA") ) continue;
                    if( line.equals("INDICES") ) continue;
                    if( line.equals(name) ) continue;

                    if( line.compareTo(name) == 1 ) {
                        contFor.add(line);
                    } else {
                        contBack.add(line);
                    }
                }
                fileBackward.put(name,contBack);
                fileForward.put(name,contFor);
            }

            String strCyclicDependencyOut = strOutputPath + "_"
                    + swPackage.getName() + "_CyclicDependency.xml";
            outFile = new PrintWriter(new BufferedWriter(new FileWriter(strCyclicDependencyOut)));
            outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            outFile.write("<CyclicDependencyFile>");

            for(Entry<String,Set<String>> entry : fileForward.entrySet()) {
                String curr = entry.getKey();
                for(String other : entry.getValue()) {
                    Set<String> otherRefs = fileBackward.get(other);
                    if( otherRefs == null ) continue;
                    if( otherRefs.contains(curr) ) {
                        outFile.write("<CyclicDepedency FileName = \""
                                + fileNames.get(curr).getPath()
                                + "\""
                                + " CyclicFileName = \""
                                + fileNames.get(other).getPath()
                                + "\" />");
                    }
                }
            }


            outFile.write("</CyclicDependencyFile>");
            outFile.flush();
            outFile.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}


Lucene comes to my mind.

Maybe it's more efficient to index all files, then query for the file names and use the results to detect your circular dependencies.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜