code: private static String replaceChars(String str)
    {
      StringBuffer buf = new StringBuffer(str);
      int length = buf.length();
      for (int i = 0; i < length; i++)
      {
        char currentChar = buf.charAt(i);
        // Replace space with "%20"
        if (currentChar == ' ')
        {
          buf.setCharAt(i, '%');
          buf.insert(i+1, "20");
          length = length + 2;
          i = i + 2;
        }
        // Replace backslash with forward slash
        else if (currentChar == '\\')
        {
          buf.setCharAt(i, '/');
        }
      }
  
      return buf.toString();
    }
docs: Replace spaces with "%20" and backslashes with forward slashes in
  the input string to generate a well-formed URI string.
  
  @param str The input string
  @return The string after conversion