【问题标题】:PrintStream doesn't print correctly unicode characters ( UTF-16)PrintStream 无法正确打印 Unicode 字符(UTF-16)
【发布时间】:2013-10-08 15:16:15
【问题描述】:

我想正确打印 unicode(比如说希腊字符),但我遇到了问题。例如:

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();

问题是是否有任何解决方案可以正确打印所有字符。我认为对于希腊字符 UTF-16 是可以的。

【问题讨论】:

  • 打印出来的内容是什么?
  • 希腊字符不是 UTF-16 吗?
  • @SotiriosDelimanolis:UTF-8 和 UTF-16 都可用于对 Unicode 中的所有字符进行编码。
  • Γειά σου = "Ο¨Ο¨Ο¨Ο¨Ο 像垃圾一样的东西
  • “我有问题”这个描述太模糊了。你到底看到了什么? client 是什么?

标签: java unicode printing utf-16


【解决方案1】:

这很可能是问题:

oStream.write(" Customer    : Γειά σου\r\n".getBytes());
oStream.write(" ΚΩΔΙΚΟΣ     : 00000234242\r\n".getBytes());

您在没有编码的情况下调用String.getBytes(),以使用平台默认编码获取字节数组。无论如何,这几乎总是一个坏主意,并且这意味着您之前指定 UTF-8 的事实与这两行无关。当PrintStream 获取数据时,它已经是二进制的了。

试试这个:

oStream.print(" Customer    : Γειά σου\r\n");
oStream.print(" ΚΩΔΙΚΟΣ     : 00000234242\r\n");

注意事项:

  • 我建议不要使用PrintStreamPrintWriter。他们吞下异常。
  • 如果你只是写文本,你应该使用Writer 子类而不是OutputStream 子类
  • 不清楚您的源代码是否得到了正确处理:您需要检查您用于编译代码的任何内容是否知道您的源文件使用的是什么编码。

我建议您将输出流包装在 OutputStreamWriter... 中,这将允许您指定编码,您不必担心意外写入二进制数据(因为 API 不允许这样做)并且你不会看到异常被吞没。

【讨论】:

  • oStream.write() 不接受字符串并强制您添加“.getBytes()”。自己试试吧!
  • @StathisAndronikos 但它有一个print(String) 方法,所以我认为这是一个错字
  • 产生相同的结果。我也有同样的问题。
  • 嗯,我必须和自己争论:打印方法记录为Prints a string. If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes **according to the platform's default character encoding**, and these bytes are written in exactly the manner of the write(int) method.
  • @ppeterka66:文档是错误的——至少根据我正在查看的实现。请记住,如果文档正确,则构造函数中指定的编码将毫无意义。
【解决方案2】:

永远不要依赖默认编码,最好总是指定,如果您想坚持使用创建字节数组的冗余方式来打印,而不是更优雅、直观和以各种方式直接使用字符串的优越方式......

oStream.write(" Customer    : Γειά σου\r\n".getBytes("UTF-8"));
oStream.write(" ΚΩΔΙΚΟΣ     : 00000234242\r\n".getBytes("UTF-8"));

来自OP的评论

客户端是一个输出流和更具体的套接字,用于将结果输出到打印机

这可能是个问题!我不相信打印机能够正确获取 UTF-8 纯字符串。请务必使用纯 ASCII 字符串对其进行测试,以确保一切正常。

编辑

Xerox WorkCentre 24 PCL 特定问题

来自PDL Reference Guide for the Xerox WorkCentre/WorkCentre Pro Series

本文档并非 PS 或 PCL 参考手册,而是在 PS、PCL 或 ASCII 打印作业中使用 WorkCentre 的扩展功能的指南。

在任何地方都没有提到 UTF-8...

来自doc at SAP: Xerox_SAP_Device_Types

要启用 Xerox® 打印机进行 Unicode 打印,请为支持 UTF-8 字符集的型号获取 Unicode 打印解决方案,其中提供了一个名为 Xerox® International Unicode Printing 的可选套件。

这听起来不太好......

【讨论】:

  • 同样的问题,但我会尝试使用 UTF-16
  • @StathisAntonikos 我不认为这会有所作为!我强烈怀疑打印机不能接受 UTF-8。
  • @StathisAndronikos 它是否适用于非常愚蠢的测试字符串?就像普通的 ASCII "ABCD" - 它打印正确吗?
  • @StathisAndronikos 是否 100% 确定打印机与 UTF-8 输入兼容?有相关文档吗?
  • 施乐 WC 24 PCL 我认为支持 UTF-8
猜你喜欢
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2016-09-07
  • 2015-03-17
  • 2018-05-12
  • 2015-11-16
相关资源
最近更新 更多