Unix のみの場合は、locate
コマンドを使用できます。updatedb
ただし、定期的に (できれば自動的に)実行することを忘れないでください。
実際に Java でコマンドライン コマンドを実行するには、この記事を参照してください。基本的なコマンドは ですがRuntime#exec
、エラー チェックを行う必要があります。この記事で提供されているスニペットは次のとおりです。
import java.io.*;
public class JavaRunCommand {
public static void main(String args[]) {
String s = null;
try {
// run the Unix "ps -ef" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("ps -ef");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}
それ以外の場合は、ファイル ツリーをウォークすることができます(NIO.2 を使用する Java ネイティブ)。ただし、キャッシュされていないため、これにはおそらくもっと時間がかかります。