如何使用Java查找字符串的循环依赖关系

时间:2011-08-08 12:19:50

标签: java

如果我有2个文件说ABCD.txt和DEF.txt。我需要检查DEF.txt中是否存在字符串“ABCD”以及ABCD.txt中是否存在字符串“DEF”,并将该组合写入文件。

我总共有大约15000个文件,每个文件包含近50 - 3000行必须被搜索。我写了一段代码,它的工作原理......但显示整个列表需要一个小时......

有更好的方法来执行此操作吗?请建议我。

   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();
    }
}

由于 RAMM

2 个答案:

答案 0 :(得分:1)

我想到了{p> Lucene

为索引所有文件,然后查询文件名并使用结果来检测循环依赖项,可能效率更高。

答案 1 :(得分:1)

您的设计存在一些问题。最重要的一个是重复扫描文件系统。请尝试下面的代码。

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();
    }
}