I'm reading a sourcefile in Java, but when I print it (sysout), the escaped characters are no longer escaped. How can I escape characters like \n
and \t
in a string in Java?

- 57,995
- 32
- 132
- 151
-
1Could you post you current code for reading and outputting? – helios Mar 09 '10 at 01:54
-
How are you reading in the source file? And how exactly are you printing them? From your description it's not obvious to me where the escape sequences are getting converted into the escape characters that they represent. – Nate C-K Mar 09 '10 at 01:55
5 Answers
You should use the StringEscapeUtils
class from Apache Commons Text (you can also find the class in Apache Commons Lang3 but that one is deprecated). You'll find that there are plenty of other offerings in Apache Commons that might serve useful for other problems you have in Java development, so that you don't reinvent the wheel.
The specific call you want has to do with "Java escaping"; the API call is StringEscapeUtils.escapeJava()
. For example:
System.out.println(StringEscapeUtils.escapeJava("Hello\r\n\tW\"o\"rld\n"));
would print out:
Hello\r\n\tW\"o\"rld\n
There are plenty of other escaping utilities in that library as well. You can find Apache Commons Text in Maven Central and you'd add it to your Maven project like this:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.3</version>
</dependency>
and in case you are using Gradle:
compile "org.apache.commons:commons-text:1.3"

- 5,567
- 5
- 33
- 55

- 1,964
- 1
- 16
- 20
Many of the solutions here suggest adding the Apache Commons Text and using StringEscapeUtils. Some of the other solutions here are simply wrong.
A potential solutions is as follows:
/**
* escape()
*
* Escape a give String to make it safe to be printed or stored.
*
* @param s The input String.
* @return The output String.
**/
public static String escape(String s){
return s.replace("\\", "\\\\")
.replace("\t", "\\t")
.replace("\b", "\\b")
.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\f", "\\f")
.replace("\'", "\\'") // <== not necessary
.replace("\"", "\\\"");
}
The escape list is from Oracle's list. (Note that \\
is escaped first as you don't want to re-escape it later.)
This solution isn't as fast as it could be, but it should work. Ideally you would only parse the String once and you wouldn't need to keep rebuilding your String array. For small Strings this should be fine.
If you're thinking about this from the perspective of storing data, also consider something like converting it to Base64 representation - it's fast, single parse and uses not too much extra space.

- 10,274
- 3
- 79
- 79

- 501
- 4
- 8
-
4.replace("\'", "\\'") is not necessary. Otherwise, thank you for this useful answer. – Comencau Feb 06 '21 at 20:20
-
I think it is necessary. Without this, a string like "\n" (a backslash followed by an "n") will be left as-is, and the target system will interpret it as a newline. – Doradus Jul 03 '21 at 02:39
-
1@Comencau It's not necessary if the string is supposed to be used in Java, but it is if the string is supposed to be used in another programming language which supports `'` as a string delimiter (like Javascript). – Donald Duck Aug 11 '21 at 10:49
Using:
\\n
and \\t
Some characters preceded by a backslash (\
) form an escape sequence and have special meaning to the compiler. So in your case \n
and \t
are treated as special (newline and tab respectively). So we need to escape the backslash to make n
and t
treated literally.

- 445,704
- 82
- 492
- 529
-
The file I'm reading contains `\n`, and I would like to programmatically convert that to `\\n` – Marius Mar 09 '10 at 02:07
Given String s
,
s = s.replace("\\", "\\\\");
Replaces all \
with \\
.

- 376,812
- 128
- 561
- 623
-
14This is wrong because \n is treated as one character so the \ will not be found to be replaced by \\ – Jonathan. Dec 11 '14 at 12:43