W3C home > Mailing lists > Public > uri@w3.org > May 2005

Re: Converting filenames to file: URIs

From: John Cowan <jcowan@reutershealth.com>
Date: Thu, 12 May 2005 10:23:55 -0400
To: Graham Klyne <gk@ninebynine.org>
Cc: uri@w3.org
Message-ID: <20050512142355.GO14557@skunk.reutershealth.com>

Graham Klyne scripsit:
> 
> I've recently been tinkering with a Java program that uses URIs, finding 
> that it doesn't work on Windows because of its filename->URI conversion 
> logic.  This has prompted me to map out a generic function to map a 
> filename to a file: URI.

Here's some code I've used:

URL fileToURL(String file) {
	char pathsep = System.getProperties("path.separator").charAt(0);
	String dir = System.getProperties("user.dir").replace(pathsep, '/');
	URL basis = new URL("file", "", dir + "/.");
	return new URL(basis, file);
	}

This has the nice property (at least on Unix and Windows) that if the
user passes a URI instead of a pathname, It Just Works.

-- 
John Cowan  www.ccil.org/~cowan  www.reutershealth.com  jcowan@reutershealth.com
'My young friend, if you do not now, immediately and instantly, pull
as hard as ever you can, it is my opinion that your acquaintance in the
large-pattern leather ulster' (and by this he meant the Crocodile) 'will
jerk you into yonder limpid stream before you can say Jack Robinson.'
        --the Bi-Coloured-Python-Rock-Snake
Received on Thursday, 12 May 2005 14:24:12 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:25:09 UTC