我的程序遇到問題。 我想按照他們的結尾對下載的文件進行排序。到目前爲止,我已經有了觀察給定文件路徑並在HashSet
中列出這些文件的結構。 現在我的問題是,該程序保持運行幾秒鐘,但隨後退出代碼0結束,所以一切都應該罰款。JVM在無故停止使用線程時關閉
public class WatchDir {
protected HashSet<File> hashSetOfFiles;
protected String filePath = "";
public WatchDir() {
chooseFilePath();
if (filePath.isEmpty())
return;
listAnfangFiles();
new Thread() {
public void run() {
listNewFiles();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
private void listNewFiles() {
File file = new File(filePath);
for (File f : file.listFiles()) {
if (hashSetOfFiles.add(f)) {
newFileFound(f);
}
}
}
private void newFileFound(File f) {
// Hier kommen alle neuen Dateien an
System.out.println(f.getName());
}
private void listAnfangFiles() {
hashSetOfFiles = new HashSet<File>();
File f = new File(filePath);
for (File ff : f.listFiles()) {
hashSetOfFiles.add(ff);
}
}
private void chooseFilePath() {
//
// JFileChooser chooser = new JFileChooser();
// chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//
// if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
// filePath = chooser.getSelectedFile().getAbsolutePath();
filePath = "C:\\Users\\maurice\\Desktop\\Test";
}
public static void main(String[] args) {
new WatchDir();
}
}
好的,謝謝。你會建議做什麼? – mauricemertens
好的,謝謝:) – mauricemertens