【发布时间】:2016-08-06 12:43:26
【问题描述】:
我的程序出现问题。
我正在尝试按结尾对下载的文件进行排序。到目前为止,我已经有了观察给定文件路径并在HashSet 中列出这些文件的结构。
我现在的问题是,程序会继续运行几秒钟,然后以退出代码 0 结束,所以一切都应该没问题。
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();
}
}
【问题讨论】:
标签: java multithreading file timer directory