Java tutorial
//package com.java2s; import java.io.IOException; import java.io.Writer; public class Main { public static void writeEscapedChar(Writer writer, char c) throws IOException { if ((c >= ' ') && (c < 0x7f)) { if ((c == '\'') || (c == '\"') || (c == '\\')) { writer.write('\\'); } writer.write(c); return; } else if (c <= 0x7f) { switch (c) { case '\n': writer.write("\\n"); return; case '\r': writer.write("\\r"); return; case '\t': writer.write("\\t"); return; } } writer.write("\\u"); writer.write(Character.forDigit(c >> 12, 16)); writer.write(Character.forDigit((c >> 8) & 0x0f, 16)); writer.write(Character.forDigit((c >> 4) & 0x0f, 16)); writer.write(Character.forDigit(c & 0x0f, 16)); } }