I am developing a java program that writtes output in a text file. When something goes wrong, I must put this ASCII art:
¯\_(ツ)_/¯
I did it with this BufferedOutputStream
:
errorOutput.writeln("##################################\n"
+ "##### Error Output ######\n"
+ "##### ¯\\_(ツ)_/¯ ######\n"
+ "##################################\n");
The problem is that when I see the txt log writted with java I get this:
##################################
##### Error Output ######
##### ¯\_(ツ)_/¯ ######
##################################
How can I write the correct ASCII emoji in Java?
Saving the .java
file as UTF-8
this code works for me:
String string = "##################################\n"
+ "##### Error Output ######\n"
+ "##### ¯\\_(ツ)_/¯ ######\n"
+ "##################################\n";
Charset.forName("UTF-8").encode(string);
System.out.println(string);
OUTPUT:
##################################
##### Error Output ######
##### ¯\_(ツ)_/¯ ######
##################################
DEMO HERE.
The file is in UTF-8, but you are viewing it in a single-byte encoding:
Ensure that you read it as UTF-8, because you are indeed using non-ASCII, comma-like, quotes and Japanese. So UTF-8 is fine.
A dirty trick under Windows would be:
String string = "\uFEFF##...
This writes a Unicode BOM char, which when being the first char of a file is interpreted as Unicode marker.
Otherwise create an HTML file with charset specified:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<pre>...</pre>
</body>
</html>
Displaying on the console, System.out, is not possible on a non-UTF-8 system like Windows.
Also for your application to be portable, make sure you specify the encoding for the writing; it often is an optional argument, with an overriden method/constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With