Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing shrugging ASCII emoji ¯\_(ツ)_/¯ in plain text with java

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?

like image 947
NullPointerException Avatar asked Aug 26 '15 14:08

NullPointerException


2 Answers

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.

like image 76
Jordi Castilla Avatar answered Nov 15 '22 16:11

Jordi Castilla


The file is in UTF-8, but you are viewing it in a single-byte encoding:

  • You are seeing UTF-8 multi-byte sequences for special chars with a char per byte.

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.

like image 21
Joop Eggen Avatar answered Nov 15 '22 17:11

Joop Eggen