英文:
Updating a JList with Files
问题
我正在为学校项目尝试制作一个小程序,用于提取文件。我正在使用一个 JList 来显示所有提取的文件,但在经过很多小时的探索后,我无法弄清楚如何在文件夹中出现新文件时使这个 JList 更新。由于批处理文件(实际提取程序)需要不同的时间来完成,它在按钮的 ActionListener 中不能正确刷新。我该如何让它工作?
这是我用于查找特定扩展名文件的类:
```java
public class fileFinder {
public static String[] thing() {
File file = new File(".\\at9snfsbs");
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith(".at9")) {
return true;
} else {
return false;
}
}
});
String[] fileNames = new String[files.length];
for (int i = 0; i < files.length; i++) {
fileNames[i] = files[i].getName();
}
return fileNames;
}
}
这是 JList:
DefaultListModel model = new DefaultListModel();
String[] things = fileFinder.thing();
for (int i = 0; i < things.length; i++) {
model.addElement(things[i]);
}
JList list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane.setViewportView(list);
这是一个按钮的 ActionListener,它启动一个批处理文件,该文件是实际的转换器:
try {
Runtime.getRuntime().exec("cmd /c start .\\at9snfsbs\\shit.bat");
String[] thang = fileFinder.thing();
model.clear();
for (int i = 0; i < thang.length; i++) {
model.addElement(thang[i]);
};
} catch (IOException e1) {
e1.printStackTrace();
}
我在编码方面没有经验或者水平不高,所以任何帮助都将不胜感激!
<details>
<summary>英文:</summary>
I'm trying to make a little program for a school project that extracts files. I'm using a JList to display all the extracted files but after lots of hours lurking I can't figure out how to make this JList update when there are new files present in the folder. It doesn't refresh properly in the button's ActionListener since the batch file (the actual extractor) takes a varied amount of time to finish. How can I get this to work?
This is the class I'm using to find files of certain extension:
public class fileFinder {
public static String[] thing() {
File file = new File(".\at9snfsbs");
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.toLowerCase().endsWith(".at9")) {
return true;
} else {
return false;
}
}
});
String[] fileNames = new String[files.length];
for (int i = 0; i < files.length; i++) {
fileNames[i] = files[i].getName();
}
return fileNames;
}
}
This is the JList:
DefaultListModel model = new DefaultListModel();
String[] things = fileFinder.thing();
for (int i = 0; i < things.length; i++) {
model.addElement(things[i]);
}
JList list = new JList(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane.setViewportView(list);
And this is a button's ActionListener that starts a batch file which is the actual converter:
try {
Runtime.getRuntime().exec("cmd /c start .\at9snfsbs\shit.bat");
String[] thang = fileFinder.thing();
model.clear();
for (int i = 0; i < thang.length; i++) {
model.addElement(thang[i]);
};
} catch (IOException e1) {
e1.printStackTrace();
}
I'm not experienced or good in coding so any help would be appreciated!
</details>
# 答案1
**得分**: 0
> Andrew Thompson: 另请参阅[当Runtime.exec()无法正常工作时](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html),了解有关正确创建和处理进程的许多有用提示。然后忽略它提到的 `exec`,并使用 `ProcessBuilder` 来创建该进程。还要将 `String arg` 拆分为 `String[] args`,以考虑包含空格字符的路径等情况。
我使用 `ProcessBuilder` 和 `waitFor()` 使其工作正常。
<details>
<summary>英文:</summary>
> Andrew Thompson: See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. Also break a `String arg` into `String[] args` to account for things like paths containing space characters.
I got it working with a `ProcessBuilder` and `waitFor()`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论