Jul 28

Java 1.5 added a replace() method that could be used to replace all occurrences of a string wiuth another string

s = s.replace(" ", "");

Prior to 1.5 you can use a regular expression with the replaceAll() method to achieve that.

s = s.replaceAll(" ", "");

// Or to replace all whitespace

s = s.replaceAll("\\s", "");

Let us know if you need a different solution.

written by objects \\ tags: , ,

Aug 03

Many developers use the toURL() method of the File class to convert a File to a URL. This method however does not handle correctly escaping characters that are illegal in URL’s (such as spaces) and has been deprecated.

To convert a File to a URL you should instead first use the toURI() method to convert the file path to a URI. The URI class has a toURL() method that can then be used to convert the URI to a URL.

File file = new File(path);
URI uri = file.toURI();
URL url = uri.toURL();

// or alternatively in one line

URL url = file.toURI().toURL();

written by objects \\ tags: , , , ,