File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to escape whitespace in file path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to escape whitespace in file path" Watch "How to escape whitespace in file path" New topic

How to escape whitespace in file path

venkatesh seshagiri

Joined: Jun 05, 2003
Posts: 7
Hi EveryBody
I am reading a file which consist of space in its name. The location of the file is
C:/Documents and Settings/venkatesh/XMLtest/ListPageDisplayPropertiesMapping.xml

The exception that i am getting is attached at the end of this message for reference. Simplest thing that can be done is i can change the directory name so that there are no spaces. However this file path is take dynamically. That is this application can run on both unix and windows. Hence the application when compiled, the executables are placed at user's home directory.

Is there a way by which i can encode the file path such that whitespaces are taken care.

Thanks for the help

S venkatesh

[2004-11-05 13:06:31 ] <main> for unknown protocol: c
at org.apache.xerces.impl.XMLEntityManager.startEntity(
at org.apache.xerces.impl.XMLEntityManager.startDocumentEntity(
at org.apache.xerces.impl.XMLDocumentScannerImpl.setInputSource(
at org.apache.xerces.parsers.DTDConfiguration.parse(
at org.apache.xerces.parsers.DTDConfiguration.parse(
at org.apache.xerces.parsers.XMLParser.parse(
at org.apache.xerces.parsers.AbstractSAXParser.parse(
at com.webobjects.appserver.xml._private._MappingModel.mappingModelWithXMLFile(
at com.webobjects.appserver.xml._private._WOXMLMappingDecoder.<init>(
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I'm not sure this is your problem (yet). It's more likely that, as the eror says, you're not using the correct protocol for the URL.

When accessing files on the file path, I believe the URL format is "file://c:/etc etc" ie we're using the 'file' protocol. THis may then lead your your space problem.
venkatesh seshagiri

Joined: Jun 05, 2003
Posts: 7
Thanks for the inputs.

Using the file as the protocal it works. That is changed the file path

C:/Documents and Settings/venkatesh/XMLtest/ListPageDisplayPropertiesMapping.xml to

file:/C:/Documents and Settings/venkatesh/XMLtest/ListPageDisplayPropertiesMapping.xml.

I would like to understand the significance of adding file:/ before the file path when there is space in the file name. When there is no space in the file name without appending the 'file:/' it works.

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The URL classes (why aren't you just opening the file directly with "new File(name)"?) might attempt to detect a filename when there's no protocol specified (the "file://"), and if so perhaps spaces make it think it's not a file.

If you really need to know, open up the code in and friends and find out.
I agree. Here's the link:
subject: How to escape whitespace in file path
jQuery in Action, 3rd edition