gpt4 book ai didi

java - 删除 JComboBox 内的文件

转载 作者:行者123 更新时间:2023-12-02 04:32:32 26 4
gpt4 key购买 nike

我有一个 JComboBox,里面装满了一些 java.io.File对象。通过在组合框中选择这些文件之一,我想从组合框和文件系统中删除它。

代码片段:

deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null, "Are you sure?", "Warning", dialogButton);

if (dialogResult == JOptionPane.YES_OPTION)
{
Path path = Paths.get(mailingLists.getSelectedItem().toString());
mailingLists.removeItem(mailingLists.getSelectedItem());

try
{
Files.delete(path);
JOptionPane.showMessageDialog(null, "File deleted!", "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e1)
{
JOptionPane.showMessageDialog(null, e1.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
}
});

它给出了这个异常:java.nio.file.FileSystemException [...] file already in use这是因为它被我的应用程序使用,然后我想首先将其从组合框中删除,然后使用 Files.delete(path); 删除它但仍然有异常(exception)。

出了什么问题?

附注

这是我第一次处理这种情况,所以我想使用 File f = new File("path"); f.delete(); 是否更好而不是Files.delete(path); .

编辑:提供有关 JComboBox 加载的更多信息。

划痕:

    LinkedList<File> listFolder = new LinkedList<File>();
listFolder.add(new File("mailinglists"));//<--- root folder

File[] stuffInFolder = listFolder.get(0).listFiles();

JComboBox<File> mailingLists = new JComboBox<File>(stuffInFolder);

最佳答案

听起来您需要关闭该文件。当您打开文件时,操作系统将阻止删除文件,直到与文件的连接关闭为止。

关于java - 删除 JComboBox 内的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31243190/

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