Here you can find the source of toURI(final String location)
public static URI toURI(final String location) throws URISyntaxException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file import java.net.URI; import java.net.URISyntaxException; import java.net.URL; public class Main { public static URI toURI(final String location) throws URISyntaxException { return new URI(replace(location, " ", "%20")); }//from www . j a va2 s .co m public static URI toURI(final URL url) throws URISyntaxException { return toURI(url.toString()); } private static String replace(final String inString, final String oldPattern, final String newPattern) { if (inString == null) { return null; } if (oldPattern == null || newPattern == null) { return inString; } final StringBuilder sbuf = new StringBuilder(); // output StringBuilder we'll build up int pos = 0;// our position in the old string int index = inString.indexOf(oldPattern); // the index of an occurrence we've found, or -1 final int patLen = oldPattern.length(); while (index >= 0) { sbuf.append(inString.substring(pos, index)); sbuf.append(newPattern); pos = index + patLen; index = inString.indexOf(oldPattern, pos); } sbuf.append(inString.substring(pos)); // remember to append any characters to the right of a match return sbuf.toString(); } }