Typically, what does it mean when java returns a “The parameter is incorrect”

烈酒焚心 提交于 2020-02-15 08:26:35

问题


I am creating a file like so

try {
    File file = new File(workingDir, obj.getName() + ".xls");
    outputStream = new FileOutputStream(file);
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
}

And I am getting

java.io.FileNotFoundException: ..\a\relative\path\obj_name.xls (The parameter is incorrect)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)

What does "the parameter is incorrect" mean? The pathname I validated exists; shouldn't it just create the file name? This is on a windows machine. The code works without this error on unix based systems.


Update

Verified that the file exists that the output stream is attempting to write to. Also verified that the file is writable. After much fussing, I removed the actual path and just passed in the file name (not desired) and that works. So the issue has something to do with the path. Do I need to escape the characters in the path?


回答1:


This looks like a reported bug on Windows machines.

Under normal situations, something like a path that has a colon (:) in it which does not refer to a drive letter would cause this message.




回答2:


It appears to be an issue with the path you're using. Try using file.getPath() before you open it to debug what is going on with your path.

File file = new File(workingDir, obj.getName() + ".xls");
System.out.println("Path="+file.getPath());
outputStream = new FileOutputStream(file);



回答3:


If your "workingDir" is a relative path, then are you sure you are on the correct "current directory" when you moved your app from unix to windows? Maybe, you should check what the current directory of the running application is.




回答4:


Perhaps the application doesn't have the correct access to write to the file? Is it read-only or otherwise protected?

FileOutputStream.open() is a native method, I would assume any sort of exception message such as "The parameter is incorrect" is coming from the underlying OS.

BTW: the File constructor does not call FileOutputStream.open(), so is the exception not actually coming from the code you posted here?




回答5:


Maybe it's because of the backslashes in the path? Path too long? File name invalid for this error (special caracters...) ?

I might be totally wrong, but worth a try since it sounds like a OS dependent error.




回答6:


Make sure the user that runs the JVM process has the right permissions to access that file.



来源:https://stackoverflow.com/questions/1208566/typically-what-does-it-mean-when-java-returns-a-the-parameter-is-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!