Writing shrugging ASCII emoji ¯\_(ツ)_/¯ in plain text with java - 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?

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:
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.

Solved with these snippets of code:
#GET
#Path("getStdErr/{idApp}")
#Produces("text/html; charset=UTF-8")
public Response getStdErr(#PathParam("idApp") Integer idApp) {
return super.getStderr(jobsMap.get(idApp));
}
.
.
.
.
return Response.ok(job.getStdErr(), "text/plain; charset=UTF-8").build();

Related

How to allow special characters like the dash in Java Application?

How to allow special characters like the dash in Java Application?
This issue is when user Copy and Paste the value from a Word Document then System shows “?” instead of dash “–” character. The copy String has dash char instead of the hyphen. Em dash and hyphen (En dash) are different characters and Em dash character is not a part of ASCII character. Its character code is \u2014. My Application does not support the dash character.
Example 1:- Not Working: Input String: "Anil – Satija" and Display Value: "Anil ? Satija"
Example 2: Name= "Anil - Satija" - hyphen is working fine and shows correct value.
Client-side technologies are Angular 1.5.5 and Server-side technologies are Spring5.2.2. Sending the request as POST. The application supports charset "UTF-8" and content-type is "application/xml". I tried to add the below code in API also to accept the all UTF-8 character set.
index.html Meta Data :
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
API Code is:
#RequestMapping(value = "/data", method = RequestMethod.POST)
#Consumes("application/xml; charset=utf-8")
public #ResponseBody
String getData(HttpServletRequest request, HttpServletResponse response) throws Throwable {
RequestPayLoad requestPayLoad = parseRequest(request);
// requestPayLoad has corrupted value.
// code
}
In debug, at the Client layer (xFactory.js) $scope.data has the correct value with dash but API layer (HttpRequest) request has the corrupted character. So, the System displays the '?' for the corrupted character in this case.

Inserting and displaying mathematical symbols in j2ee web application

I have mathematical symbols e.g. alfa, beta,mu . When I copy these symbols in text area they are getting copied. I am copying them from word document. When I insert them into the database using prepared statement the symbols are getting inserted as code. for example the alfa is getting stored as β. This is fine I guess. But when I retrieve them from the database using java.sq.Statement and displaying them in the html page they are getting displayed as code instead of symbol. I mean "β" is displayed in html instead displaying alfa symbol. So how to deal with this situation? how can I store symbols and display them properly in html?
I am using mysql database, java1.7,struts2.0 and tomcat7.
The correct display of HTML characters is: β (Looks like: β) You need to add a semicolon.
1) How are you displaying the codes in HTML?
2) What is the char encoding of machine your are running your server/viewing your html
I had following code and it worked
<html>
<body>
This is alpha α<br/>
This is beta β <br/>
This is gamma Γ <br/>
<body>
</html> as shown below:
This is alpha α
This is beta β
This is gamma Γ
You may need to declare your charset:
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
or see the encoding of your server (if its in jsp)
The following tag of struts helped me solving this to an extent.
<s:property value="name" escape="false" />
I hope you're using JSPs. Add this import on top of your JSP which is rendering the symbols:
<%# page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

Special Characters In Webapp being saved differently

I'm creating a webapp using Spring MVC and some of the information I'm pulling is from a Database, so it was edited elsewhere. When I import some have, what I consider, special characters, such as
“_blank”
as opposed to using the standard keyboard
"_blank".
When I display this on my website textarea, it displays fine, but when I attempt to save it back into the string when submitting the form in the spring textArea, the string now has ? where the 'special' characters were. They were obviously imported into a String fine, but somewhere in the save process it's not allowing it as a special character. Any idea what is causing this or why?
Sounds like a character encoding problem. Try setting the character set of the page containing the form to UTF-8.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

xtag issue with special characters

I'm using x tag to parse through an xml that has special characters such as é Here is my xml
<stack>
<data title="thé"/>
</stack>
here is the xtag that prints out the output
<x:out select="#title" />
the view source of the page displays this output
theé
and visually this is displayed by the browser
theé
What am I doing wrong and how do I fix this issue?
Since the source view shows the character correctly, the problem is probably not with your JSTL XML tag expression. Instead, it might have to do with the content-type that the page is labeled with.
Single non-ASCII characters getting rendered as two characters (the first is typically an A with some sort of accent) is a pretty sure sign that UTF-8 content is getting treated as ISO-8859-1, or something similar. I'm not an expert in this area, but the browser needs to be told that the content you're serving is in UTF-8. So check the meta content-type of your output. It should specify UTF-8:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

How to generate Chinese characters from a Java servlet?

My servlet looks like this
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
PrintWriter out=response.getWriter();
out.println("<Html><Head><Title>Signup</Title></Head>\n<Body>\n");
out.println("\u5982 电话\n");
out.println("</Body>\n</Html>");
}
My browser can display Chinese characters from other websites.
I'm trying 2 different ways to display Chinese characters, but they all showed up as ???
What's the correct way to do it ?
No explicit encoding has been set for the response. The response would therefore be written by the container with the default encoding of ISO-8859-1.
You'll therefore need to specify the appropriate character encoding using the HttpServletResponse.setCharacterEncoding() or HttpServletResponse.setContentType methods. This would be either of:
response.setCharacterEncoding("GB18030");
response.setContentType("text/html; charset=GB18030");
You may also use UTF-8 as the explicit encoding.
Try adding
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
You would need to send Unicode, have your servlet send UTF-8, and have the browser locale set up properly to interpret the characters correctly.
Just setting the character encoding as UTF-8 worked for me.
response.setCharacterEncoding("UTF-8")

Categories