Spaces creates problem in Metro

Alexandros Paramythis alpar at
Thu Mar 31 05:11:42 EST 2005

Niclas Hedhman wrote:

> Hi,
> Makas is reporting the following problem on a Windows system
> Apparently spaces in filenames are trouble.

This is a (relatively) well-known problem on Windows. It is typically 
encountered with 'file:' URLs, which do not properly encode spaces in 
several cases.

The available solutions, if I recall correctly are:

* If the URL gets created from a File object, and it is known that one 
ultimately needs a URI, then one can invoke f.toURI() instead of 
f.toURL(). Apparently, the former performs the encoding properly.

* A variation of the first solution, and assuming that there indeed 
exists a File object from which the URL gets created is to call 
f.toURI().toURL() which results in a properly encoded URL.

* If the URL is composed rather than retrieved from f.toURL(), or it is 
not possible to apply the above solutions for any reason, the only 
realistic solution I am aware if involves the use of a method somewhat 
like the following (very rough, only intended to demonstrate the 
principle) when converting from a URL to a URI:

public static URI convertToURI(URL url) throws URISyntaxException {
	// stores the original exception (if any) to be rethrown
	// (if necessary)	
	URISyntaxException originalException = null;
	// attempt straightforward conversion
	try {
		return new URI(url.toExternalForm());
	} catch (URISyntaxException e) {
		// syntax error, attempt to compensate, after
		// storing the original exception to use later
		// if necessary
		originalException = e;

	// fix problems commonly occuring with 'file:' URLs on
	// Windows
	if ("file".equals(url.getProtocol())) {
		return new File(url.getFile()).toURI();

	// last ditch effort to handle URLs with non-encoded spaces
	try {
		return new URI(
			url.toExternalForm().replaceAll(" ", "%20"));
	} catch (URISyntaxException e) {
		// syntax error, all is lost
	// throw the original exception for consistency
	throw originalException;


I hope this helps.



Alexandros Paramythis

Institute for Information Processing and Microprocessor Technology (FIM)
Johannes Kepler University Linz
Altenbergerstr. 69, A-4040 Linz, AUSTRIA

Tel: +43 (0) 732 2468 8442
Fax: +43 (0) 732 2468 8599
Email: alpar at

More information about the dev-dpml mailing list