This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes File access : blanks in file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File access : blanks in file name" Watch "File access : blanks in file name" New topic
Author

File access : blanks in file name

Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

I have a problem accessing files which contain blanks in the file name.
I also tried URLEncoder.encode() but this doesnt work as there are + chars
inserted :

Can't follow link to http://localhost/HTML/Links/html Notgeld - Wikipedia.htm: java.io.FileNotFoundException: http://localhost/HTML/Links/html+Notgeld+-+Wikipedia.htm

What is the trick to access files with blanks in the name ? Or is this forbidden ?

Also the ! char generates an error message.

Time isn't Money, Money is Time.
The man who works looses his time ( spanish proverb )
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

First of all, those aren't files. They are URLs -- Universal Resource Locators. So they locate resources, not files.

And second, you use %20 in place of a space in a URL, not the + sign. For other non-letter, non-number characters you URL-encode them.
Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

1. Sorry for my confusion. You are right of course.

2. Dont quite understand what to do now.
Does it mean that I have to replace every blank
by the three chars %20 in my url string variable ?
Is there an API method for that ?
And how to URL-encode the other chars ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

java.net.URLEncoder


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You haven't explained why you have these not-quite-correct URLs in the first place. If you're getting them from somewhere else, then you're getting bad data. Or are you making them up yourself? How?

In other words I'm suggesting (based on no evidence of what you are actually doing) that you should just get correct URLs in the first place, instead of trying to repair broken URLs.
Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

@Bear

I really dont understand what is written in URLEncoder doc.
I used the encode() method and it places the + in my url string.

@Paul

The names were generated by me when I used a Web editor
to create web pages : I gave them file names with blanks and
other chars as the ! or ' for instance.
Since I now have many of the files I cant rename them all manually.

So I need a way to access them programmatically by using setPage()
method foer instance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So you basically did it wrong in the first place and now you have to fix it up. So I would recommend a quick-and-dirty fix which replaces spaces by %20 and those other characters by whatever URL-encoding would replace them by.

And your Web editor didn't URL-encode your file names properly?
Wolfgang Tintemann
Ranch Hand

Joined: Sep 11, 2010
Posts: 65

No, my Evrsoft web editor ( from 2006 !!! ) allows me
to enter blanks in the file name under Save As menu item.

I now found a thread on the Ranch which said to use
the replaceAll() method.

So I replaced all blanks with %20 and now all works fine.

Thank you so much for your effort.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File access : blanks in file name
 
Similar Threads
SOS
Context paths always coming up as "The requested resource is not available"
where to put war
how to deploy a .war file?
JSP