2011-08-08 4 views
1

2つのファイルにABCD.txtとDEF.txtがあるとします。文字列 "ABCD"がDEF.txtに存在するかどうか、またABCD.txtに "DEF"という文字列があるかどうかを確認し、その組み合わせをファイルに書き込む必要があります。Javaを使用して文字列の循環依存関係を見つける方法

私には約15000個のファイルがあり、各ファイルには50〜3000行程度が含まれています。私はコードを書いていましたが、そのリストを表示するには1時間かかります...

これを実行する方法はありますか?私に提案してください。

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

+0

他の特定の部分からクリーンアップされ、純粋な問題自体のためにのみ集中するコードフラグメントをここにコピーすることが不可欠です。現在のコードの責任は多すぎます。遅いの本当の理由が何であるかを知るのは難しいです。 – pcjuzer

+0

@pcjuzer:コード全体をコピーして、コード化されたものが正しいことを確認しました。それがあなたを混乱させるなら、Plsは私を許します。実際には、フォルダ内の各ファイルを読み込み、ファイル内の各行をチェックしています。ファイル行がファイルの場合は、そのファイルに移動し、最初のファイルが存在する場合はその行をチェックします。次に、それらをXMLファイルに書き出します。 – Ramm

答えて

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

こんにちはSimon、更新されたコードありがとう。しかし、この行で "if(otherRefs.contains(curr)" .. java.lang.NullPointerExceptionが見られ、 "otherRefs"の値がnullを出力しています – Ramm

+0

これは、現在のファイルの行が参照していないためです –

+0

null値が返されたときにそのコードを追加しましたが、どういうわけか、コントロールがif(otherRefs.contains() curr))..出力ファイルにはヘッダ部分以外の行はありません。 "OtherRefs"の値は常にnullです。ありがとう – Ramm

1

Luceneは私の心に来ます。

すべてのファイルのインデックスを作成し、ファイル名をクエリし、結果を使用して循環依存を検出する方が効率的かもしれません。

+0

Luceneは良いアイデアかもしれませんが、ファイルのコーパスが静的で頻繁に変更されない場合にのみそうです。それ以外の場合は、Luceneインデックスの作成時に検索しながら保存した時間が失われます。 – fgysin

+0

フルインデックスは3分以内に作成する必要があります。その後、サイクル検出のために57分が残った;) –

関連する問題