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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7398521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:47:21  来源:igfitidea点击:

java.io.IOException: The system cannot find the path specified

javaio

提问by Maik

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?

但正如标题所说,我从 desktop.open(file) 结构中得到一个“java.io.IOException:系统找不到指定的路径”。问题肯定是文件路径名包含空格(被翻译成“%20”)。有没有办法避免这种情况?

回答by Maik

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.

我发现了真正的问题。它不是我想象的 %20。我只是没有直接访问文件位置的权限。解释起来有点复杂……对不起,我以前无法弄清楚真正的问题。

Thanks for your suggestions anyway!

无论如何,感谢您的建议!

回答by krslynx

Are you using an IDE? What is inside the variable 'filename' (it's actual contents). Line two is unnecessary.

你用的是IDE吗?变量“文件名”中的内容(它是实际内容)。第二行是不必要的。

Is the error from the stack trace pointing to BufferedWriter bw = new BufferedWriter(new FileWriter(file));or desktop.open(file);

堆栈跟踪中的错误是否指向BufferedWriter bw = new BufferedWriter(new FileWriter(file));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.

出现 java.io 错误是因为它无法打开文件。上面的代码将强制以您的文件作为参数打开 excel。您需要设置环境变量以确保命令行中的命令“excel”打开 Excel 应用程序。

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.

如果您计划发布此应用程序以供使用,您可以通过检查注册表来确保安装了 Excel,然后从那里检查 Excel 的安装位置。

回答by Kris

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

尝试使用其他应用程序打开不同的文件,看看是否支持其他文件类型。正如 Clarisse 所说,如果指定的文件没有关联的应用程序或关联的应用程序无法启动,则 'open' 方法会抛出 IOException。如果指定的文件不存在,则抛出 IllegalArgumentException,这不是您的情况。如果由于某种原因无法使用桌面打开 CSV 文件,请尝试使用 krslynx 方法。同样可以在这里找到。您可以使用此处找到的代码快速组装测试应用程序以打开您机器上的任何内容

回答by Thomas

In the Desktop javadoc it's written :

在桌面 javadoc 中它是这样写的:

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 ?

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

回答by user207421

As krslynx says, file.createNewFile() is unnecessary. However file.mkdirs() may be necessary instead, if the intermediate directories don't exist yet.

正如 krslynx 所说, file.createNewFile() 是不必要的。但是,如果中间目录尚不存在,则可能需要 file.mkdirs()。

EDIT: it's not clear from your question whether this is happening in new FileWriter() or in Desktop.open(). Please clarify.

编辑:从您的问题中不清楚这是否发生在 new FileWriter() 或 Desktop.open() 中。请澄清。