aspose file tools*
The moose likes I/O and Streams and the fly likes Spaces in a URI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Spaces in a URI" Watch "Spaces in a URI" New topic
Author

Spaces in a URI

N D Fisher
Greenhorn

Joined: Jun 01, 2007
Posts: 27
Hi,

I'm trying to create a URI for a file in my Documents and Settings directory but I always get an error saying:

java.net.URISyntaxException: Illegal character in path...etc.

Now I know that the path name has spaces in it but I have replaced those with '%20' in order to avoid any errors - and yet I still get an exception on the space. Can anyone explain why?

For example, if I try:

URI uri = new URI("file://C:/temp/New%20Folder/myfile.tmp");


Then I get:

java.net.URISyntaxException: Illegal character in path at index 18: file://C:/temp/New%20Folder/myfile.tmp

Is there any way around this?

Many thanks,

Nick.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Works for me. Strange.

Anyway, using java.io.File you can do this a lot more robustly:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
N D Fisher
Greenhorn

Joined: Jun 01, 2007
Posts: 27
Thanks.

After a lot of fiddling about, I did actually manage to get it to work. The way I discovered was:

URI uri = new URI("file", "", "/c:/temp/New Folder/myfile.tmp", "");

Which creates the URI without throwing any exceptions.

However, I'm now in another dilemma. The reason I needed to create the URI was so I could browse for a file like:

desktop.browse(uri);

But even though the URI now gets created correctly, it still doesn't browse for it. No errors or exceptions, it just doesn't do anything. Even this doesn't work:

desktop.browse("file:///c:/myfile.html");

But I guess that's for another thread!!

Cheers anyway,

Nick.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spaces in a URI