views:

717

answers:

6

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  A: 

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?

matt b
I updated my code, forgot an important piece. outputStream = new FileOutputStream(file);
predhme
You should probably check if file.exists() returns true before you attempt to write to it
matt b
verified that the file exists and is writtable and still getting the error (The parameter is incorrect) being thrown outputStream = new FileOutputStream(file);
predhme
A: 

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.

marcgg
You might also want to try changing the relative path into an absolute one, windows can be weird with that sometimes...
marcgg
+1  A: 

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.

JeffH
we are using a relative path in this case.
predhme
Did you look at the "reported bug" link in my answer? I looks like what is happening to you.
JeffH
it is, not sure how to work around it. it doesnt like the path that the file api generates.
predhme
So I am basically running into the bug you mentioned. I wasn't able to find a workaround that I particularly liked... What I ended up doing was creating a temp file in the . directory then copying that file to where it should be then cleaning up the temp file. Thanks!
predhme
+1  A: 

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);
Doldrim
..\tmp\renderdfiles\1343488-1249043441342-xq58o\GT-10 - 2009-07-31 08:30:24.xls
predhme
That is the actual path that is being passed to the outputstream.
predhme
+1  A: 

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.

Blessed Geek
they are forced to start the application in a specific directory. otherwise there would be many other errors.
predhme
A: 

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

df
yea, other processes are creating files and directories in the same location (different code)
predhme