code: public void printJavaString(String s) throws IOException
    {
      for (int i = 0; i < s.length(); i++) {
        char ch = s.charAt(i);
  
        switch (ch) {
        case '\\':
          _os.print("\\\\");
          break;
        case '\n':
          _os.print("\\n");
          break;
        case '\r':
          _os.print("\\r");
          break;
        case '"':
          _os.print("\\\"");
          break;
        default:
          _os.print(ch);
        }
      }
    }
docs: Prints a Java escaped string