public static String escapeJavaString(String s){
  StringBuilder sb=new StringBuilder();
  for (int i=0; i < s.length(); i++) {
    char ch=s.charAt(i);
switch (ch) {
case '\\':
      sb.append("\\\\");
    break;
case '\n':
  sb.append("\\n");
break;
case '\r':
sb.append("\\r");
break;
case '"':
sb.append("\\\"");
break;
default :
sb.append(ch);
}
}
return sb.toString();
}

public static String escapeJavaString ( String s ) { StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char ch = s . charAt ( i ) ; switch ( ch ) { case '\\' : sb . append ( "\\\\" ) ; break ; case '\n' : sb . append ( "\\n" ) ; break ; case '\r' : sb . append ( "\\r" ) ; break ; case '"' : sb . append ( "\\\"" ) ; break ; default : sb . append ( ch ) ; } } return sb . toString ( ) ; }
prints a java escaped string
