所以,我正试图在Jlist中列出我计算机上的所有文本文件。我创建了以下算法,问题是它没有完成,我的处理器达到了100% 。谁能建议更好的实施? 这是我的代码:
public AllFilesFrame() {
initComponents();
setTitle("All Files");
jList1.setModel(model);
getDirectories("/");
}
private void getDirectories(String directory){
File file = new File(directory);
File[] directories = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
});
if(directories != null){
for(int i=0;i<directories.length;i++){
listFiles(directories[i]);
getDirectories(directories[i].toString());
}
}
}
private void listFiles(File folderName){
File f = new File(folderName.toString());
FilenameFilter textFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
};
File[] files = f.listFiles(textFilter);
if(files != null){
for(File file : files){
if(file.isFile()){
model.addElement(file);
}
}
}
}