09 November 2005

Replacing backslash with double backslash

This is a juvenile problem and too Microsofty. The problem is when Windows-based file names has been stored in a properties file in this way:


When retrieving that value, the backslashes will be dropped during Properties.load() execution so the resulting string will be:


Sometimes we have GUI-based configuration managers that we like to be able to store in a properties file or an XML file but want to retrieve the file names with desirable results. Therefore our configuration should look like this:


In order to achieve that, we can use a 2-liner code with JDK's Regular Expression API like this one:

String path = "C:\Windows\Kamote";
String newPath = path.replaceAll("\\\\", "\\\\\\\\");

The code above will yield C:\\Windows\\Kamote. This code is not really necessary, it's just a convenience fix for Windows dummies.


Richard said...



this should be better

Jared said...

Will equally work but not better. The problem with forward slash is that SWT and Swing FileDialog boxes doesn't give filenames in such formats and Windows dummies are not used to forward slashes.

Anjan said...

hi there,

Xemacs/emacs on windows can read bother forward/back slashes -- that's very convenient.

I use sed to do my regexes inside of batch files.