gpt4 book ai didi

java - JFileChooser 返回 FileNotFoundException

转载 作者:行者123 更新时间:2023-11-29 05:45:07 24 4
gpt4 key购买 nike

更具体地说,它确实有效,但前提是我选择了一个存在于我的程序及其资源所在的源文件夹中的文件。当我将文件移动到桌面或我的文档并尝试从那里读取它时,我得到一个 FileNotFoundException

这是我的代码:

private void btnBrowseFileActionPerformed(java.awt.event.ActionEvent evt) {                                              
JFileChooser myFileChooser = new JFileChooser();
int rVal = myFileChooser.showOpenDialog(Singlelayer.this);
if (rVal == JFileChooser.APPROVE_OPTION) {
txtFile.setText(myFileChooser.getSelectedFile().getName());
}
}

如您所见,它附加到“浏览...”按钮,因此它是 GUI 的一部分。但这不是重点。

它不适用于与其他源文件不在项目文件夹中的任何文件。不完全确定发生了什么,但任何帮助将不胜感激。

最佳答案

您正在使用文件名:

txtFile.setText(myFileChooser.getSelectedFile().getName());

返回文件名。所以它只能识别源文件夹中的文件。

相反,您应该使用 file path .

关于java - JFileChooser 返回 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16108769/

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