Re: Converting filenames to file: URIs

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