问题
I want to print correctly unicode (let's say greek characters ) but I have problems. For example :
PrintStream oStream = new PrintStream(client.getOutputStream(), true, "UTF-8");
oStream.write(" Customer : Γειά σου\r\n".getBytes());
oStream.write(" ΚΩΔΙΚΟΣ : 00000234242\r\n".getBytes());
oStream.flush();
oStream.close();
OR
OutputStreamWriter oStream = new OutputStreamWriter(client.getOutputStream(), "UTF-16");
oStream.write(" Customer : Γειά σου\r\n");
oStream.write(" ΚΩΔΙΚΟΣ : 00000234242\r\n");
oStream.flush();
oStream.close();
The question is if there is any solution to print correctly all of the caharacters. I think for Greek characters UTF-16 is ok.
回答1:
This is quite possibly the issue:
oStream.write(" Customer : Γειά σου\r\n".getBytes());
oStream.write(" ΚΩΔΙΚΟΣ : 00000234242\r\n".getBytes());
You're calling String.getBytes()
with no encoding, to get a byte array using the platform default encoding. That's almost always a bad idea anyway, and it means that the fact that you specified UTF-8 earlier is irrelevant to these two lines. By the time the PrintStream
gets the data, it's already in binary.
Try this instead:
oStream.print(" Customer : Γειά σου\r\n");
oStream.print(" ΚΩΔΙΚΟΣ : 00000234242\r\n");
Notes:
- I would advise against using either
PrintStream
orPrintWriter
. They swallow exceptions. - If you're only writing text, you should use a
Writer
subclass rather than anOutputStream
subclass - It's unclear whether your source code is even being handled correctly: you need to check that whatever you're using to compile your code knows what encoding your source file is using.
I suggest you wrap your output stream in an OutputStreamWriter
... that will allow you to specify the encoding, you won't have to worry about accidentally writing binary data (as the API doesn't allow it) and you won't see exceptions getting swallowed.
回答2:
Never rely on the default encoding, much better to always specify, if you want to stick with the redundant way of creating a byte array to print, instead of the much more elegant, intuitive, and in every way superior way of using the String directly...
oStream.write(" Customer : Γειά σου\r\n".getBytes("UTF-8"));
oStream.write(" ΚΩΔΙΚΟΣ : 00000234242\r\n".getBytes("UTF-8"));
From comment of OP
client is an output stream & more aspecific a socket to output the result to a printer
That might be an issue! I wouldn't trust printers to get the UTF-8 plain string correctly. Be sure to test it with plain, ASCII strings to make sure everything else is OK.
EDIT
Xerox WorkCentre 24 PCL specific issues
From the PDL Reference Guide for the Xerox WorkCentre/WorkCentre Pro Series:
This document is not intended as a PS or PCL reference manual, but a guide for using the extended features WorkCentre’s in PS, PCL or ASCII print jobs.
There is not a single mention of UTF-8 anywhere...
From this doc at SAP: Xerox_SAP_Device_Types
To enable the Xerox® printer for Unicode printing, obtain the Unicode Printing Solution for models which support the UTF-8 character set, where an optional kit is available called Xerox® International Unicode Printing.
This doesn't sound good...
来源:https://stackoverflow.com/questions/19251688/printstream-doesnt-print-correctly-unicode-characters-utf-16