java.io.IOException:系统找不到指定的路径

发布于 2024-12-04 10:09:08 字数 505 浏览 0 评论 0原文

我正在尝试打开我刚刚在代码中创建的文件(所以我确信该文件存在)

代码是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪异←思 2024-12-11 10:09:08

我发现了真正的问题。
这也不是我想象的%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!

紧拥背影 2024-12-11 10:09:08

您使用 IDE 吗?变量“文件名”里面有什么(它是实际内容)。第二行是不必要的。

堆栈跟踪中的错误是否指向 BufferedWriter bw = new BufferedWriter(new FileWriter(file));desktop.open(file);

编辑:

您也可以尝试以下代码

File myCSVFile; //reference to your csv file here 
String execString = "excel " + myCSVFile.getAbsolutePath();
Runtime run = Runtime.getRuntime();
try {
    Process pp = run.exec(execString);
} catch(Exception e) {
    e.printStackTrace();
}

出现 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)); or desktop.open(file);

EDIT:

You can also try the following code

File myCSVFile; //reference to your csv file here 
String execString = "excel " + myCSVFile.getAbsolutePath();
Runtime run = Runtime.getRuntime();
try {
    Process pp = run.exec(execString);
} catch(Exception e) {
    e.printStackTrace();
}

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.

得不到的就毁灭 2024-12-11 10:09:08

尝试使用其他应用程序打开不同的文件,看看是否支持其他文件类型。正如 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

无需解释 2024-12-11 10:09:08

在桌面 javadoc 中写道:

IOException - if the specified file has no associated application or the associated application fails to be launched 

那么您确定您的文件类型有关联的默认应用程序吗?

In the Desktop javadoc it's written :

IOException - if the specified file has no associated application or the associated application fails to be launched 

So are you sure your filetype has a default application associated ?

肤浅与狂妄 2024-12-11 10:09:08

正如 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文