简体   繁体   English

如何在Jar文件中获取Unicode

[英]How to get Unicode in Jar files

I wrote a program for calling Unicode from outside the program. 我编写了一个程序,用于从程序外部调用Unicode。 I am using Windows XP and Eclipse. 我正在使用Windows XP和Eclipse。 When I run that program in the IDE, it shows the Unicode, but when I exported it as a .jar file I am unable read the Unicode and it shows as boxes. 当我在IDE中运行该程序时,它显示Unicode,但是当我将其导出为.jar文件时,我无法读取Unicode,并且显示为方框。

I followed these instructions to install Unicode in my computer. 我按照以下说明在计算机上安装Unicode。 I followed links to install the Telugu fonts to my system. 我点击了将泰卢固语字体安装到我的系统的链接。

Can any one please tell me how can I get Unicode in Jar files? 谁能告诉我如何在Jar文件中获取Unicode?

While not answering the question directly, here is a small howto on how to read/write correctly from text files, in an OS dependent way. 虽然没有直接回答问题,但这里有一个小技巧,介绍了如何以依赖于OS的方式从文本文件正确读取/写入。

First thing to know is that the JVM has a file.encoding property. 首先要知道的是,JVM具有file.encoding属性。 It defines the default encoding used for all file read/write operation, all readers used when not specifying an encoding. 它定义了用于所有文件读/写操作的默认编码,未指定编码时使用的所有读取器。

As such, you don't want to use the default constructors, but define the encoding each time. 因此,您不想使用默认的构造函数,而是每次都定义编码。 In Java, the class which "embodies" an encoding is Charset . 在Java中,“体现”编码的类是Charset If you want UTF-8, you will use: 如果要使用UTF-8,将使用:

  • StandardCharsets.UTF_8 (Java 7+), StandardCharsets.UTF_8 (Java 7+),
  • Charset.forName("UTF-8") (Java 6-), Charset.forName("UTF-8") (Java 6-),
  • Charsets.UTF_8 (if you use Guava). Charsets.UTF_8 (如果使用番石榴)。

In order to read a file correctly, open an InputStream to that file, then an InputStreamReader over that InputStream (in the code samples below, UTF8 is the UTF-8 charset obtained from one of the methods above): 为了正确读取文件,请打开该文件的InputStreamReader ,然后打开该InputStream InputStream (在下面的代码示例中, UTF8是从上述方法之一获得的UTF-8字符集):

final InputStream in = new FileInputStream(...);
final Reader reader = new InputStreamReader(in, UTF8);

In order to write a file correctly, open an OutputStream to it, then an OutputStreamWriter over that OutputStream : 为了正确地写一个文件,打开OutputStream给它,那么OutputStreamWriter在那OutputStream

final OutputStream out = new FileOutputStream(...);
final Writer writer = new OutputStreamWriter(out, UTF8);

And, of course, do not forget to .close() both of the streams/readers/writers in a finally block. 并且,当然,不要忘记在finally块中同时 .close() 两个流/读取器/写入器。 Hint: if you don't use Java 7, use Guava 14.0+, use Closer . 提示:如果您不使用Java 7,请使用Guava 14.0+,请使用Closer It is the most secure way to deal with multiple I/O resources and ensuring they are dealt with correctly. 这是处理多个I / O资源并确保对其进行正确处理的最安全方法。

You did the code already (I didn't follow the links), but you may compare the code with How to import a font - registerFont is crucial. 你做的代码已经(我没有按照链接),但你可以比较的代码如何导入字体 - registerFont是至关重要的。

Also in a jar file all paths are case-sensitive . 同样在jar文件中,所有路径都区分大小写 You may inspect the jar with 7zip or WinZip. 您可以使用7zip或WinZip检查罐子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM