gpt4 book ai didi

java - 将文件夹中的文件列出到 JOptionPane

转载 作者:行者123 更新时间:2023-11-30 08:51:08 24 4
gpt4 key购买 nike

最近我一直在开发一个程序来列出文件夹中的文件,以便它们显示在 JOptionPane 上。 .

我试图获得 File[]调用File.listFiles在文件夹对象上,但结果是 NullPointerException在线for (int i = 0; i < listOfFiles.length; i++) { .

我的代码是这样的:

if (OS.contains("Mac")){
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File folder = new File("/Users/"+ user +"/Desktop");

File[] listOfFiles = folder.listFiles();
String herd = "";
for (int i = 0; i < listOfFiles.length; i++) {
StringBuffer sb = new StringBuffer(2000);
String s = listOfFiles[i].getName();
herd = sb.append(s).append(", ").toString();
}

msgbox(herd);

}
});
}

消息框是:

public void msgbox(String s){
JOptionPane.showMessageDialog(null, s);
}

NullPointerException堆栈跟踪如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at goatSoftware.CreateJFrame$1.actionPerformed(CreateJFrame.java:93)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

试试这个:

if(OS.contains("Mac"))

{
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String path = "/Users/" + user + "/Desktop";
File folder = new File(path);

File[] listOfFiles = folder.listFiles();
if (listOfFiles != null && listOfFiles.length > 0) {
StringBuffer sb = new StringBuffer(2000);
for (int i = 0; i < listOfFiles.length; i++) {
String s = listOfFiles[i].getName();
sb.append(s).append(", ");
}
msgbox(sb.toString());

} else {
msgbox("No files in the folder " + path);
}
}
});
}

初始代码中的错误:

  1. 您在 for 循环中声明了 StringBuffer。在每个循环中都会构造一个新对象。
  2. 不需要在每个循环中执行 sb.toString()。通常只做一次。
  3. File.listFiles() 可能会为无效路径返回 null。所以 listOfFiles 的值应该被评估为 null

需要改进的地方:

  1. “无效路径”和“空目录”的不同消息。现在一切都一样,但不应该。

  2. 尾随逗号!对于包含一些文件的有效目录,输出将是:“a, b, c,”。最后一个逗号是不必要的。您必须以某种方式避免或删除它,这是一个单独的(也是一个非常受欢迎的)问题。祝你好运。 )

关于java - 将文件夹中的文件列出到 JOptionPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com