ファイルがいっぱいのフォルダを繰り返し処理し、テキストを抽出するアプリケーションがあります。アプリケーションが処理したファイルをログに記録し、プログラムを再実行するときに、同じフォルダー内のファイルをスキップして、既にテキストを抽出しておきたい。現時点では、処理されたファイルをログに記録することができますが、プログラムを再実行するとファイルが再処理され、すべての処理が遅くなります。以下に何が間違っていて、より効率的な方法がありますか?ファイルを処理する際にすでに処理されたファイルをスキップする方法
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();
}
}
}
}
}
'myFiles'の内容をデバッグして調べてみてください。ファイル名とファイルシステムのファイル名に多少の違いがあるかもしれません。 –
あなたの問題から独立しています:Java標準ライブラリに 'java.util.Iterator'クラスがあります。私はクラスを標準クラスのように命名しない – JimHawkins