java.io.IOException:系统找不到指定的路径
我正在尝试打开我刚刚在代码中创建的文件(所以我确信该文件存在)
代码是这样的:
File file = new File(filename);
file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
...
bw.close();
try {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.open(file);
} catch (Exception e) {
...
}
但正如标题所说,我得到一个“java.io.IOException:系统找不到路径从desktop.open(file)指令中指定”。 问题肯定是文件路径名包含空格(被翻译为“%20”)。有办法避免这种情况吗?
I am trying to open a file i just created in my code (so i am sure that the file exists)
The code is like this:
File file = new File(filename);
file.createNewFile();
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
...
bw.close();
try {
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
desktop.open(file);
} catch (Exception e) {
...
}
But as the title says i get a "java.io.IOException: The system cannot find the path specified" from the desktop.open(file) istruction.
The problem surely is that the file pathname contains spaces (which are translated into "%20"). Is there a way to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我发现了真正的问题。
这也不是我想象的%20。
我只是没有直接访问文件位置的权限。解释起来有点复杂...
我只是很抱歉我之前无法弄清楚真正的问题。
不管怎样,谢谢你的建议!
I found the real problem.
It wasn't either the %20 as i supposed.
I just hadn't the privileges to directly access the file location. It's a bit complicated to explain...
i'm just sorry i coulnd't figure out the real problem before.
Thanks for your suggestions anyway!
您使用 IDE 吗?变量“文件名”里面有什么(它是实际内容)。第二行是不必要的。
堆栈跟踪中的错误是否指向
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
或desktop.open(file);
编辑:
您也可以尝试以下代码
出现 java.io 错误,因为它无法打开文件。上面的代码将强制以您的文件作为参数打开 Excel。您需要设置环境变量以确保命令行中的命令“excel”打开 Excel 应用程序。
如果您打算发布此应用程序以供使用,您可以通过检查注册表来确保 Excel 已安装,然后从注册表中检查 Excel 的安装位置。
Are you using an IDE? What is inside the variable 'filename' (it's actual contents). Line two is unnecessary.
Is the error from the stack trace pointing to
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
ordesktop.open(file);
EDIT:
You can also try the following code
The java.io error is appearing because it's failing to open the file. The code above will force excel open with your file as the argument. You'll need to set your environment variable to ensure that the command 'excel' in the command line opens the Excel application.
If you're planning on releasing this application for use you can ensure that excel is installed by checking the registry, then checking the install location of Excel from there.
尝试使用其他应用程序打开不同的文件,看看是否支持其他文件类型。正如 Clarisse 所说,如果指定的文件没有关联的应用程序或关联的应用程序无法启动,则“open”方法会抛出 IOException。如果指定的文件不存在,则会抛出 IllegalArgumentException,这不是您的情况。如果由于某种原因无法使用 Desktop 打开 CSV 文件,请尝试使用 krslynx 方法。可以在此处找到相同内容。您可以使用找到的代码快速组装一个测试应用程序以打开计算机上的任何内容
Try to open a different file with other applications and see if other file types are supported. As Clarisse said, IOException is thrown from the 'open' method if the specified file has no associated application or the associated application fails to be launched. If the specified file doesn't exists IllegalArgumentException is thrown, which is not in your case. If for some reason opening a CSV file with Desktop doesn't work for you, try using krslynx approach. Same can be found here. You can quickly assemble a test application for opening anything on your machine using the code found here
在桌面 javadoc 中写道:
那么您确定您的文件类型有关联的默认应用程序吗?
In the Desktop javadoc it's written :
So are you sure your filetype has a default application associated ?
正如 krslynx 所说, file.createNewFile() 是不必要的。但是,如果中间目录尚不存在,则可能需要 file.mkdirs() 。
编辑:从您的问题中不清楚这是发生在 new FileWriter() 还是 Desktop.open() 中。请澄清。
As krslynx says, file.createNewFile() is unnecessary. However file.mkdirs() may be necessary instead, if the intermediate directories don't exist yet.
EDIT: it's not clear from your question whether this is happening in new FileWriter() or in Desktop.open(). Please clarify.