0

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?

3
  • 3
    On which line you get the exception? are you using absolute paths? Are both snippets within same class/package? Commented Sep 13, 2011 at 7:58
  • Exc comes form desktop.open. Using relative paths (starting from ./ ). Same class for both snippets Commented Sep 13, 2011 at 8:52
  • Edited original message. It surely depends on %20 Commented Sep 13, 2011 at 9:25

5 Answers 5

3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

6 Comments

Yes i am using netbenas. The filename variable contains tha path of the file (it's a string). The error comes from desktop.open
OKay, so in Netbeans the file will be relative to /src, however you might want to take a look at this bug: bugs.sun.com/view_bug.do?bug_id=6764271 Please paste the exact path that you're using.
I edited the question. What i did forgot to mention is that the same code worked for another file. The problem is 100% that the file path contains spaces, which are converted to "%20". So i just need to solve that problem! I know that it's getting a little confused here now... maybe i should create abother question?
I see, so I gather that the Desktop.open method requires a File as the argument, and not a string that you can provide as a literal or use a URLEncoder/Decoder. Thus, the most reasonable answer I can provide is to do something like this: Construct the URI first: download.oracle.com/javase/6/docs/api/java/net/URI.html Create the File with the URI as the argument: download.oracle.com/javase/6/docs/api/java/io/File.html Use Desktop.open with the File as the argument: download.oracle.com/javase/6/docs/api/java/awt/… Hope this helps.
Also, the URI should be something like: "file://C:\Program\ /Files/Another\ /File\ /With\ /Spaces\ /In\ /The\ /url.csv"
|
1

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

4 Comments

I have edited the original question. I just have to solve the problem which converts blank spaces into "%20", after that it will work (i have tested it)
use URLDecoder/Encoder, have a look here: stackoverflow.com/questions/5474345/…
I can't understand what do you mean... The desktop.open() wants a File, not a path (a String). I guess that if it wanted a string i could manually edit it
You have just said that you have some file path problems with %20, if so, try to encode your fileName. Before calling desktop.open() try File.exists() - download.oracle.com/javase/6/docs/api/java/io/…
0

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 ?

1 Comment

Well i guess it has. It's a .csv file, and it opens by default with excel
0

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.

1 Comment

The problem it's opening the file. The file it's created succesfully and i can open it manually afterwards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.