2016-10-20 7 views
0

ファイルがいっぱいのフォルダを繰り返し処理し、テキストを抽出するアプリケーションがあります。アプリケーションが処理したファイルをログに記録し、プログラムを再実行するときに、同じフォルダー内のファイルをスキップして、既にテキストを抽出しておきたい。現時点では、処理されたファイルをログに記録することができますが、プログラムを再実行するとファイルが再処理され、すべての処理が遅くなります。以下に何が間違っていて、より効率的な方法がありますか?ファイルを処理する際にすでに処理されたファイルをスキップする方法

public class Iterator { 
    static HashSet<String> myFiles = new HashSet<String>(); 
    public static Preferences prefs; 
    static String filename= "/Files/FilesLogged.txt"; 
    static String folderName; 
    static Path p; 
    public Iterator() { 
    } 

    public static void main(String[] args) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException, BackingStoreException {  
     Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(ClassLoader.class.getResourceAsStream(filename)),2048); 
     String line = null; 
     //Reading the files from the logger so they can be avoided 
     while((line = reader.readLine()) != null) { 
      myFiles.add(line); 
     } 


      //This iterates through each of the files in the specified folder and copies them to a log. 
      //It also checks to see if that file has been read already so that it isn't re-inputted into the database if run again    
      //Loop through the ArrayList with the full path names of each folder in the outer loop 

      String[] keys = userPrefs.keys(); 
      for (String folderName : keys) { 
       //Extract the folder name from the Prefs and iterate through 
       if(userPrefs.get(folderName, null)!=null){ 
         loopthrough(userPrefs.get(folderName, null)); 
       } 
      } 
      reader.close(); 
}    





public static void loopthrough(String folderName) throws IOException, SAXException, TikaException, SQLException, ParseException, URISyntaxException{ 

     File dir = new File(folderName); 
     File[] directoryListing = dir.listFiles();   
      if (directoryListing != null) { 
       for (File child : directoryListing) { 

         if(!myFiles.contains(child.getName())){ 
         Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class); 
         FileWriter fw= new FileWriter(userPrefs.get("PathForLogger", null),true); 

            BufferedWriter bw = new BufferedWriter(fw,2048); 
            bw.write(child.getName().toString().trim()); 
            bw.newLine(); 
            bw.flush(); 
            bw.close(); 
            fw.close(); 

                   } 
                } 
              } 
     } 

} 
+0

'myFiles'の内容をデバッグして調べてみてください。ファイル名とファイルシステムのファイル名に多少の違いがあるかもしれません。 –

+1

あなたの問題から独立しています:Java標準ライブラリに 'java.util.Iterator'クラスがあります。私はクラスを標準クラスのように命名しない – JimHawkins

答えて

1

は通常、次の操作を行います。処理を開始すると は、あなたが最初にすることは..inprocessまたは類似のものか、インプロセスディレクトリに移動するファイルを移動しています。 処理が終了したら、名前を..doneなどに変更するか、完了したディレクトリに移動します。 こうすることで、処理するファイルを探すときに、処理中のファイルと完了済みのファイルを避けることができます。また、再処理が必要なものを簡単に確認できます

0

私は、プログラムが読み書きする2つの異なるファイルがあると思います。

  1. 読書のためのファイル:

    新しいをBufferedReader(新しいInputStreamReaderの(ClassLoader.class.getResourceAsStream(ファイル名))、2048);

  2. 書き込み用のファイル:

    設定ユーザー設定= Preferences.userNodeForPackage(TBB_SQLBuilder.class)。

    FileWriter fw = new FileWriter(userPrefs.get( "PathForLogger"、null)、true);

もちろん、プログラムは異なるファイルを使用してはいけません。

0

検査するファイルが20個以下のテスト環境を作成します。

あなたのコードに変更し

String line = null; 
    //Reading the files from the logger so they can be avoided 
    while ((line = reader.readLine()) != null) 
    { 
     myFiles.add(line); 
     System.out.println("already processed: "+line); 
    } 
 for (File child : directoryListing) 
     { 
      String fileToCheck = child.getName(); 
      System.out.println("file to process: "+fileToCheck); 
      if (!myFiles.contains(fileToCheck)) 
      { 
       Preferences userPrefs = Preferences.userNodeForPackage(TBB_SQLBuilder.class); 
       FileWriter fw = new FileWriter(userPrefs.get("PathForLogger", null), true); 

       BufferedWriter bw = new BufferedWriter(fw, 2048); 
       bw.write(fileToCheck.trim()); 
       bw.newLine(); 
       bw.flush(); 
       bw.close(); 
       fw.close(); 
      } 
     } 

のファイル名は "処理済み" と "チェックするファイル" の比較。

または、デバッガを使用してください。

関連する問題