code: public static String escapeNewlines(String str) {
      final int len = str.length();
      StringWriter out = new StringWriter(len * 2);
      for (int i = 0; i < len; i++) {
        char c = str.charAt(i);
        switch (c) {
          case '\\':
            out.write('\\');
            out.write('\\');
            break;
          case '\n':
            out.write('\\');
            out.write('n');
            break;
          default:
            out.write(c);
        }
      }
      return out.toString();
    }
docs: Escapes new line characters of a given string.
  It also escapes the forward slash characters.
  @param str string to be escaped
  @return escaped string