gpt4 book ai didi

java - 文件路径无效异常

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:51 24 4
gpt4 key购买 nike

我的应用程序抛出异常 - java.io.FileNotFoundException:文件路径无效。不知道为什么。我已阅读有关该主题的问题和答案,但没有人可以帮助我。

这是代码:

    String userhome = System.getProperty("user.home");
String filename = null;
File rdp = null;
for (int item = 0; item < darab; item++) {
filename = toValidFileName(ProgramList.get(item).getP_name());
filename += ".rdp";
rdp = new File(userhome, filename);
try {
JFrame panel;
panel = new JFrame();
panel.setSize(400, 10);
panel.setLocation(300, 400);
panel.setTitle("Saving " + rdp.getAbsolutePath());

try (FileOutputStream fstr = new FileOutputStream(rdp)) {
panel.setVisible(true);
char c;
for (int j = 0; j < 2336; j++) {
c = ProgramList.get(item).p_body.charAt(j);
fstr.write(c);
}
fstr.flush();
fstr.close();
panel.setVisible(false);
}

} catch (IOException ioe) {
JOptionPane.showMessageDialog(this,
ioe.getMessage(), "Save rdp file", JOptionPane.ERROR_MESSAGE);
System.err.println(ioe.getMessage() + " : "+ rdp.getAbsoluteFile());
}
}

结果:无效文件路径:C:\Users\LiPI\CosmicLd.rdp

toValidFilename() 是从 (KORG RADIAS) 程序名称中删除禁用字符以创建有效的文件名。

我还没找到我的错:(目标目录不是只读的,用户具有必要的权限。当我在该行之后查看 file.canWrite() 时: rdp = 新文件(用户主目录,文件名);它总是假的。我做错了什么?谢谢!

最佳答案

尝试使用文件写入器。您的尝试将如下所示:

try {
File dir = new File("C:/Users//" + userhome + "/Documents"); //customize this however
FileWriter fstr = new FileWriter(new File(dir, rdp));
for (int j = 0; j < 2336; j++) {
c = ProgramList.get(item).p_body.charAt(j);
fstr.write(c);
}
fw.close();
}

此外,请尝试遵守 Java 变量命名约定。 userhome 应该是 userHome 等。只是一件小事:)

关于java - 文件路径无效异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42627779/

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